熊猫:沿着特定的列切片后,无需返回整个数据框即可获取“值”

时间:2018-12-03 15:21:14

标签: python pandas

这是正在发生的事情:

df = pd.read_csv('data')

important_region = df[df.columns.get_loc('A'):df.columns.get_loc('C')]

important_region_arr = important_region.values

print(important_region_arr)

现在,这是问题所在:

print(important_region.shape)
output: (5,30)

print(important_region_arr.shape)
output: (5,30)

print(important_region)
output: my columns, in the panda way

print(important_region_arr)
output: first 5 rows of the dataframe

在索引了列之后,如何过渡到numpy数组?

或者,我可以从一开始就转换为numpy并在numpy中运行切片操作。但是,这是如何在大熊猫中完成的?

1 个答案:

答案 0 :(得分:1)

因此,这是如何使用特定列对数据集进行切片。 loc使您可以访问行和列的组。 ,之前的数字表示其后的行和列。如果指定了:,则表示所有行。

data.loc[:,'A':'C']

要了解更多信息,请查看documentation