Reputation: 49
I have a dataframe 'data' that looks like this:
<bound method NDFrame.head of Close ... Volume
A AA TSLA ... A AA TSLA
Date ...
2020-06-24 86.378616 11.14 960.849976 ... 1806600 7562700 10959600
2020-06-25 87.077148 11.83 985.979980 ... 1350100 6728600 9254500
2020-06-26 85.720001 10.93 959.739990 ... 2225800 25817600 8854900
2020-06-29 87.290001 10.99 1009.349976 ... 1302500 7397600 9026400
2020-06-30 88.370003 11.24 1079.810059 ... 1920200 5796600 16881600
[5 rows x 15 columns]>
Now, from this dataframe, I would like to get all the data for 'A' into a single dataframe.
I can do this via:
df2['Open'] = data['Open']['A']
df2['High'] = data['High']['A']
df2['Low'] = data['Low']['A']
etc.
And that works fine... However, there must be a smarter way to do this, right?
All help appreciated!
Upvotes: 2
Views: 69
Reputation: 862406
Sure, use DataFrame.xs
for selecting in MultiIndex
:
df2 = data.xs('A', axis=1, level=1)
Upvotes: 1