I have a dataframe with 100 columns and I want to select list of variables
ID A B C
0 day1 day10 Δday day1 day10 Δday day1 day10 Δday
1 1 1.0 2.0 1.0 1.5 2.5 1.0 3.0 2.0 -1.0
2 2 3.0 5.0 2.0 1.0 2.5 1.5 3.0 5.0 2.0
3 3 2.5 3.5 1.0 1.5 2.9 1.4 3.5 4.0 0.5
............................................................
............................................................
df.columns
Index(['A', 'Unnamed: 1', 'Unnamed: 2', 'B', 'Unnamed: 4', 'Unnamed: 5', 'C','Unnamed: 7', 'Unnamed: 8'],
dtype='object')
How do I filter which should yield the output as:
A B C
Δday Δday Δday
1.0 1.0 -1.0
2.0 1.5 2.0
1.0 1.4 0.5
I looked at Pandas' documentation and tried filter regex, iloc but did not succeed.
CodePudding user response:
Try slice(None)
to select any column at first level:
>>> df.loc[:, (slice(None), 'Δday')]
A B C
Δday Δday Δday
0 1.0 1.0 -1.0
1 2.0 1.5 2.0
2 1.0 1.4 0.5
To know more: Using slicers
Update
Tried to applied and getting TypeError: unhashable type: 'slice'
Your problem is because you don't use read_excel
correctly. Your second row is not a column header but a simple row. Try to specify the header:
df = pd.read_excel('data.xlsx', ..., header=[0, 1])