按子列将数据帧拆分为多个数据帧 [英] Split Dataframe to multiple Dataframes by sub columns
问题描述
我找不到拆分此表的优雅方法:
I could not find an elegant way to split this table :
Open Close High
stock1 stock2 stock1 stock2 stock1 stock2
.. .. .. .. .. ..
当股票是子栏时,一个股票分成多个表格,所以第一个表格是
Where stocks are sub columns, into multiple tables each for a stock, so first table is
df_stock1
Open Close High
因此每个股票的表/数据框(可以是 > 2 )然后返回这些 dataframes
的数组.
so a table/dataframe for each stock ( could be > 2 ) then return an array of those dataframes
.
推荐答案
你可以这样做.这是一个虚构的数据框:
You could do something like this. Here's a fictiv dataframe:
import pandas as pd
import numpy as np
np.random.seed(123)
df = pd.DataFrame(np.random.randint(1, 10, (6, 6)),
columns=pd.MultiIndex.from_product([['Open', 'Close', 'High'],
['Stock1', 'Stock2']]))
打印:
Open Close High
Stock1 Stock2 Stock1 Stock2 Stock1 Stock2
0 3 3 7 2 4 7
1 2 1 2 1 1 4
2 5 1 1 5 2 8
3 4 3 5 8 3 5
4 9 1 8 4 5 7
5 2 6 7 3 2 9
然后要拆分您的多索引 df,请使用 groupby 和 DataFrame.xs 执行以下操作:
Then to split your multi-index df, do the following, using groupby and DataFrame.xs:
Split = {idx: df_sub.xs(idx, level=1, axis=1) for idx,df_sub in df.groupby(level=1, axis=1)}
print(Split['Stock1'])
其中 df_sub
指的是多索引 df 的第一级.这给出了:
where df_sub
refers to the first-level of the multiindex df. This givess:
Open Close High
0 3 7 4
1 2 2 1
2 5 1 2
3 4 5 3
4 9 8 5
5 2 7 2
和
Split2 = {idx: df_sub.xs(idx, level=1, axis=1) for idx,df_sub in df.groupby(level=1, axis=1)}
print(Split2['Stock2'])
印刷品:
Open Close High
0 3 2 7
1 1 1 4
2 1 5 8
3 3 8 5
4 1 4 7
5 6 3 9
编辑其他级别
同样,如果您想要所有未公开的股票,您可以这样做:
Similarily, if you want all the Open stocks, you can do this:
Split_open = {idx: df_sub.xs(idx, level=0, axis=1) for idx,df_sub in df.groupby(level=0, axis=1)}
print(Split_open['Open'])
返回:
Stock1 Stock2
0 3 3
1 2 1
2 5 1
3 4 3
4 9 1
5 2 6
这篇关于按子列将数据帧拆分为多个数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!