在 Python Pandas 中选择两个索引值之间的 DataFrame 行
我们可以切片PandasDataFrame来选择两个索引值之间的行。让我们举一个例子,看看它是如何完成的。
步骤
创建二维、大小可变、潜在异构的表格数据df。
打印输入数据帧df。
为索引的下限初始化一个变量。
为索引的上限初始化另一个变量。
使用df[index_lower_limit:index_upper_limit]打印范围索引中的DataFrame。
示例
import pandas as pd
df = pd.DataFrame(
{
"x": [5, 2, 7, 0],
"y": [4, 7, 5, 1],
"z": [9, 3, 5, 1]
}
)
print "Input DataFrame is:\n", df
index_lower_limit = 1
index_upper_limit = 3
print("DataFrame between two index values:\n",
df[index_lower_limit: index_upper_limit])输出结果Input DataFrame is: x y z 0 5 4 9 1 2 7 3 2 7 5 5 3 0 1 1 DataFrame between two index values: x y z 1 2 7 3 2 7 5 5
热门推荐
10 新人红包祝福语简短精辟
11 幼师给毕业祝福语简短
12 生日高考祝福语妹妹简短
13 下飞机祝福语简短英文
14 元宵发客户祝福语简短
15 爸妈金婚文案祝福语简短
16 新郎朋友红包祝福语简短
17 虎年五一祝福语大全简短
18 出阁宴祝福语姐姐简短