简体   繁体   English

根据pandas数据帧中的列标签对数据进行分组

[英]Group data based on column label in pandas dataframe

I've been reading about hierarchical index and multiindex in a pandas dataframe but it seems these are all for ordered labels. 我一直在阅读pandas数据框架中的分层索引和多索引,但似乎这些都是有序标签。 For example, my data looks like this: 例如,我的数据如下所示:

在此输入图像描述

And I want to be able to group the data together based on the column label ie. 我希望能够根据列标签将数据分组在一起,即。 aggregate all columns with 'd' in row 3 together by averaging. 通过平均将第3行中所有列与'd'聚合在一起。

What is the best way to get this excel data (or csv if absolutely needed) into a dataframe so that I can do these operations and how would I go about doing them? 将这个excel数据(或csv,如果绝对需要)放入数据帧的最佳方法是什么,以便我可以执行这些操作以及如何进行这些操作?

Any advice or references would be appreciated 任何建议或参考将不胜感激

EDIT 编辑

I tried loading the data from a csv using the following command: 我尝试使用以下命令从csv加载数据:

data = pd.read_csv('Dataset.csv', index_col=0, header=[0,1,2,3], parse_dates=True)

which gives me this when loaded: 加载时给我这个:

<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 18 entries, 2013-05-27 10:31:00 to 2013-07-24 11:31:00
Data columns (total 40 columns):
(1, mix, d, n)     18  non-null values
(2, aq, s, n)      18  non-null values
(3, gr, s, n)      18  non-null values
(4, mix, d, n)     18  non-null values
(5, aq, d, n)      17  non-null values

I'm just not really sure where to go from there. 我只是不确定从那里去哪里。

You can use column-wise ( axis=1 ) groupby and take the mean : 您可以使用逐列( axis=1groupby并取mean

In [11]: df = pd.DataFrame(np.random.randn(4, 3), columns=[[1, 2, 3], ['d', 's', 'd']])

In [12]: df.columns.names = ['PLOT', 'DEPTH']

In [13]: df
Out[13]:
PLOT          1         2         3
DEPTH         d         s         d
0     -0.557490 -1.231495 -0.333703
1      0.513394  1.046577  0.596306
2     -0.404606 -1.615080 -0.694562
3     -0.078497 -0.683405  0.056857

In [14]: df.groupby(level='DEPTH', axis=1).mean()
Out[14]:
DEPTH         d         s
0     -0.445596 -1.231495
1      0.554850  1.046577
2     -0.549584 -1.615080
3     -0.010820 -0.683405

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM