我如何从熊猫中按排序时间序列索引的数据框中的列中的所有值生成统计特征?

问题描述 投票:0回答:1

我从医院获得了孩子的出生数据,并要求在其中执行某些任务:

时间戳记种族性别body_mass

01:03:27 indian m 8.1

01:07:20 hispanic f 5.9

01:09:34 romani m 7.2

... ... ... ...

11:56:15 irish f 6.3

并且我需要每10分钟为“种族”中的每个值生成统计特征。

timestamp indian_avg indian_max indian_min ... iris_min

01:20:00 7.1 9.5 4.7 ... 5.1

01:40:00 7.2 8.8 5.6 ... 6.9

... ... ... ... ... ...

12:00:00 7.6 10.1 5.1 ... 6.7

[请帮助我是一个初学者,并且在此问题上停留了一天]

python pandas time-series feature-extraction
1个回答
0
投票

您可以使用pd.Grouper!并按频率和种族分组。

df.groupby([pd.Grouper(freq='10min'), 'ethnicity']) \
  .agg({'body_mass': ['max', 'min']})

为了获得所需的准确格式,可以执行以下操作以获得所需的结果(更多信息,请参见Pandas - How to flatten a hierarchical index in columns

df.groupby([pd.Grouper(freq='10min'), 'ethnicity']) \
  .agg({'body_mass': ['max', 'min']}) \
  .unstack()
df.columns = [' '.join(col).strip() for col in df.columns.values]
© www.soinside.com 2019 - 2024. All rights reserved.