通过列索引而不是列名来调用数据框中的列-Pandas

bal*_*lla 5 python dataframe pandas

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

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

除了调用之外df['a'],我可以使用其列索引来调用它df[1]吗?

Mik*_*ler 6

您可以使用 iloc

df.iloc[:, 0]
Run Code Online (Sandbox Code Playgroud)

例:

>>> 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
Run Code Online (Sandbox Code Playgroud)

  • 使用`df.groupby(df.columns [1])`。 (2认同)