Pandas: How to get position of columns?

流过昼夜 提交于 2020-01-02 07:13:09

问题


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.

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

回答1:


IIUC you can use .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'


来源:https://stackoverflow.com/questions/33941406/pandas-how-to-get-position-of-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!