pandas 熊猫:如何获得列的位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33941406/
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-09-14 00:16:39  来源:igfitidea点击:

Pandas: How to get position of columns?

pandaspositionmultiple-columns

提问by A.Boss

I need help to get the position of the column or another way to read in the column two step left of the column Spannung.

我需要帮助来获取列的位置或以其他方式读取列Spannung左侧两步的列。

Exceldata = pd.read_excel(str(Dateien[0]), header=[2])
print Dateien[0]
Spannung = Exceldata.columns[Exceldata.columns.str.contains('Spannung effektiv L1')]
print Spannung

采纳答案by EdChum

IIUC you can use .get_loc

IIUC你可以使用 .get_loc

So:

所以:

pos = Exceldata.columns.get_loc(Spannung[0])

then you can index left:

然后你可以索引左边:

other_col = Exceldata.columns[pos -2]

Example:

例子:

In [169]:
df = pd.DataFrame(columns=['hello','world','python','pandas','Spannung effektiv L1', 'asdas'])
spannung = df.columns[df.columns.str.contains('Spannung')]
spannung

Out[169]:
Index(['Spannung effektiv L1'], dtype='object')

In [178]:
pos = df.columns.get_loc(spannung[0])
df.columns[pos-2]

Out[178]:
'python'