Python 按列索引而不是列名调用数据框中的列 - 熊猫

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36316154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:44:04  来源:igfitidea点击:

Call column in dataframe by column index instead of column name - pandas

pythonpandasdataframe

提问by balakishore nadella

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

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

For example I have dataframe dfwith columns a, b, c

例如,我有数据帧df的列abc

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

df['a']我可以使用它的列索引来调用它,而不是调用它df[1]吗?

回答by Mike Müller

You can use iloc:

您可以使用 iloc

df.iloc[:, 0]

Example:

例子:

>>> 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