Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: How to get position of columns?

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
like image 553
A.Boss Avatar asked Nov 26 '15 14:11

A.Boss


1 Answers

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'
like image 109
EdChum Avatar answered Oct 21 '22 13:10

EdChum