通过列索引而不是列名来调用数据框中的列-Pandas [英] Call column in dataframe by column index instead of column name - pandas

查看:91
本文介绍了通过列索引而不是列名来调用数据框中的列-Pandas的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用数据框中的索引而不是其名称来调用代码中的列。

How can I call column in my code using its index in dataframe instead of its name.

例如,我有数据框 df ,其中列 a b c

For example I have dataframe df with columns a, b, c

代替调用 df ['a'] ,我可以使用其列索引(如<$)来调用它吗? c $ c> df [1] ?

Instead of calling df['a'], can I call it using its column index like df[1]?

推荐答案

您可以使用 iloc

You can use iloc:

df.iloc[:, 0]

示例:

>>> df
   a  b  c
0  1  4  7
1  2  5  8
2  3  6  9

>>> df['a']
0    1
1    2
2    3
Name: a, dtype: int64

>>> df.iloc[:, 0]
0    1
1    2
2    3
Name: a, dtype: int64

这篇关于通过列索引而不是列名来调用数据框中的列-Pandas的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆