Reputation: 2319
I have a PANDAS dataframe with a two line index. I want to keep certain columns based on the second line index. How could I do it? I have tried a few things but nothing worked. For example, consider the following dataframe:
AAA BBB CCC
C1 C2 C3 C1 C2 C3 C1 C2 C3
Index
1 1 2 3 4 5 6 1 2 3
2 2 5 0 7 8 9 7 4 5
3 7 4 1 5 7 2 2 5 9
How can I keep only column C2 for all cases (i.e. for AAA, BBB, CCC). As a result I would like to have:
AAA BBB CCC
C2 C2 C2
Index
1 2 5 2
2 5 8 4
3 4 7 5
Thanks in advance for the help.
Upvotes: 1
Views: 165
Reputation: 3847
Let me just give you an example:
df = pd.DataFrame(np.random.randint(9,size=(3,9)))
df.columns = pd.MultiIndex.from_product([['AAA','BBB','CCC'],['C1','C2','C3']])
AAA BBB CCC
C1 C2 C3 C1 C2 C3 C1 C2 C3
0 8 3 7 2 8 7 1 8 2
1 8 3 1 8 5 2 0 1 0
2 4 0 0 5 8 4 7 1 5
df.iloc[:, df.columns.get_level_values(1)=='C2'] # note get_level_values()
AAA BBB CCC
C2 C2 C2
0 3 8 8
1 3 5 1
2 0 8 1
Upvotes: 3