[英]Split a multi-index dataframe in dataframes by column names
I have a dataframe like the following: Multi-index dataframe by columns我有一个如下所示的数据框:按列的多索引数据框
I would like to get 3 dataframes named like each columns (compass, accel, gyro) with the timeindex untouched, and three columns each(df1, df2, df3).我想获得 3 个数据帧,命名为每列(指南针、加速、陀螺仪),时间索引未受影响,每列三列(df1、df2、df3)。
I've tried for index,row in df.iterrows():
but couldnt really got it to work And I was thinking in somenthing stack()
and unstack()
but don't really know how.我已经尝试
for index,row in df.iterrows():
但无法真正让它工作而且我在想stack()
和 unstack unstack()
但真的不知道如何。
You can save the 3 first columns in a csv file, and repeat the process more 2 times to the others csv files...您可以将前 3 列保存在 csv 文件中,然后对其他 csv 文件重复该过程 2 次...
You can select the 3 columns to your dataframe like this:您可以像这样选择数据框的 3 列:
x = 0
data=pd.read_csv(file.csv, keep_default_na=False, skiprows=line_header, na_filter=False, usecols=[x,x+1,x+2])[[compass, accel, gyro]])
where x = your first column of the "big dataframe"其中 x = “大数据框”的第一列
the usecols property is really useful in this case在这种情况下 usecols 属性非常有用
You can read more about in: Pandas.read_csv您可以在以下位置阅读更多信息: Pandas.read_csv
groupby
allows you to split the DataFrame along a MultiIndex level with the same level_values. groupby
允许您沿具有相同 level_values 的 MultiIndex 级别拆分 DataFrame。 We will use DataFrame.xs
to remove the grouping Index level, leaving you with only the columns you care about.我们将使用
DataFrame.xs
删除分组索引级别,只留下您关心的列。 Separate DataFrames are stored in a dictionary, keyed by the unique level-1 values of the original column MultiIndex.单独的 DataFrame 存储在字典中,由原始列 MultiIndex 的唯一级别 1 值键控。
import pandas as pd
import numpy as np
np.random.seed(123)
df = pd.DataFrame(np.random.randint(1, 10, (4, 9)),
columns=pd.MultiIndex.from_product([['df1', 'df2', 'df3'],
['compass', 'gyro', 'accel']]))
# df1 df2 df3
# compass gyro accel compass gyro accel compass gyro accel
#0 3 3 7 2 4 7 2 1 2
#1 1 1 4 5 1 1 5 2 8
#2 4 3 5 8 3 5 9 1 8
#3 4 5 7 2 6 7 3 2 9
d = {idx: gp.xs(idx, level=1, axis=1) for idx,gp in df.groupby(level=1, axis=1)}
d['gyro']
# df1 df2 df3
#0 3 4 1
#1 1 1 2
#2 3 3 1
#3 5 6 2
As such splits are readily available with a groupby
you may not even need to store the separate DataFrames;由于这样的拆分可以通过
groupby
轻松获得,您甚至可能不需要存储单独的 DataFrame; you can manipulate each of them separately with GroupBy.apply
.您可以使用
GroupBy.apply
分别操作它们中的每一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.