python - Pandas :沿特定列切片后,获取 "values"而不返回整个数据帧

标签 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 中运行切片操作。但是,这在 pandas 中是如何完成的?

最佳答案

下面介绍了如何使用特定列对数据集进行切片。 loc 使您可以访问行和列组。 之前的代表行和之后的列。如果指定了 :,则表示所有行。

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

更多理解请看documentation .

关于python - Pandas :沿特定列切片后,获取 "values"而不返回整个数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53596692/

相关文章:

Python 使用部分测试属性

在不同的有限参数上重复方法调用的 Pythonic 方式

python - FuzzyWuzzy - 循环列表,匹配接受的值,并返回数据帧

python - 为什么 groupby sum 不将 boolean 转换为 int 或 float?

python - Pandas 数据帧 : change duplicated rows so the first duplicates are in consecutive order

python - 按 Pandas 中的位置提取数字?

python - numpy中两个一维向量的点积

Python:在 pyppeteer 中保持打开浏览器并创建 CDPSession

python - Pandas :旋转数据框

python - 如何在具有图例和次要 y 轴的同一图上绘制两个 Pandas 时间序列?