添加 groupby 对象的单个数据框的数字列的 Pythonic 方法

Posted

技术标签:

【中文标题】添加 groupby 对象的单个数据框的数字列的 Pythonic 方法【英文标题】:Pythonic way to add numeric columns of the individual dataframes of a groupby object 【发布时间】:2020-08-27 07:56:57 【问题描述】:

我有一个要分组的时间序列数据,我想将所有组的数字列相加。

注意:这不是单个组列的聚合,而是组对象中所有数据框的相应单元格的总和。

由于它是一个时间序列数据,因此在 RegionRegion_CodeTime 这样的数据框中,有几列本质上保持不变。

我的伪代码是-

    分组Region_Code 仅选择分组对象的数字列 制作区域列表 通过遍历区域列表和求和来调用组对象中的数据框 将其他列设为RegionRegion_CodeTime

但问题是,当我用一个空数据框添加被调用的数据框时,一切都变成了空/空,所以最终我什么都没有。

import pandas as pd

countries = ['United States','United States','United States','United States','United States', 'Canada', 'Canada', 'Canada', 'Canada', 'Canada', 'China', 'China', 'China', 'China', 'China']
code = ['US', 'US','US','US','US','CAN','CAN','CAN','CAN','CAN', 'CHN','CHN','CHN','CHN','CHN']
time = [1,2,3,4,5,1,2,3,4,5,1,2,3,4,5]
temp = [2.1,2.2,2.3,2.4,2.5, 3.1,3.2,3.3,3.4,3.5, 4.1,4.2,4.3,4.4,4.5]
pressure = [1.0,1.0,1.0,1.0,1.0, 1.1, 1.1, 1.1, 1.1, 1.1, 1.2,1.2,1.2,1.2,1.2]
speed = [20,21,22,23,24, 10,11,12,13,14, 30,31,32,33,34]

df = pd.DataFrame('Region': countries, 'Time': time, 'Region_Code': code, 'Temperature': temp, 'Pressure': pressure, 'Speed': speed)

countries_grouped = df.groupby('Region_Code')[list(df.columns)[3:]]

country_list = ['US', 'CAN', 'CHN']

temp = pd.DataFrame()
for country in country_list:
    temp += countries_grouped.get_group(country) ## <--- Fails

temp

# Had the above worked, the rest of the columns can be made as follows
temp['Region'] = 'All'
temp['Time'] = df['Time']
temp['Region_Code'] = 'ALL'

它看起来并不讨人喜欢。最好的方法是什么?

预期输出

    Region  Time    Region_Code     Temperature     Pressure    Speed
0   All      1          ALL              9.3            3.3       60
1   All      2          ALL              9.6            3.3       63
2   All      3          ALL              9.9            3.3       66
3   All      4          ALL              10.2           3.3       69
4   All      5          ALL              10.5           3.3       72

【问题讨论】:

预期输出 DataFrame 的外观如何? @jezrael 添加了预期输出 【参考方案1】:

我认为您需要聚合 sum - 默认情况下排除所有非数字列,因此您可以通过 DataFrame.reindex 将它们添加到原始列中,并通过 ALL 替换缺失值:

print (df.groupby('Time', as_index=False).sum())
   Time  Temperature  Pressure  Speed
0     1          9.3       3.3     60
1     2          9.6       3.3     63
2     3          9.9       3.3     66
3     4         10.2       3.3     69
4     5         10.5       3.3     72

df = df.groupby('Time', as_index=False).sum().reindex(df.columns, axis=1, fill_value='ALL')
print (df)
  Region  Time Region_Code  Temperature  Pressure  Speed
0    ALL     1         ALL          9.3       3.3     60
1    ALL     2         ALL          9.6       3.3     63
2    ALL     3         ALL          9.9       3.3     66
3    ALL     4         ALL         10.2       3.3     69
4    ALL     5         ALL         10.5       3.3     72

编辑:对于自定义替换缺失值,使用带有字典的DataFrame.fillna - 带有替换值的列名:

d = 'Region':'GLOBAL','Region_Code':'ALL'
df1 = df.groupby('Time', as_index=False).sum().reindex(df.columns, axis=1).fillna(d)
print (df1)
   Region  Time Region_Code  Temperature  Pressure  Speed
0  GLOBAL     1         ALL          9.3       3.3     60
1  GLOBAL     2         ALL          9.6       3.3     63
2  GLOBAL     3         ALL          9.9       3.3     66
3  GLOBAL     4         ALL         10.2       3.3     69
4  GLOBAL     5         ALL         10.5       3.3     72

【讨论】:

非常接近。实际上有两列RegionRegion_Code,它们实际上采用不同的值而不是ALL。我可能应该使用更好的例子。假设Region 我想要GLOBAL 和Region_Code 我想要ALL,那么我该怎么办?

以上是关于添加 groupby 对象的单个数据框的数字列的 Pythonic 方法的主要内容,如果未能解决你的问题,请参考以下文章

装箱后无法访问数据框的 groupby 对象的各个列

当percentile_approx基于groupby返回特定列的单个值时,如何选择另一列的对应值?

如何在 pandas groupby 对象上应用函数并将结果保存回父数据框的新列?

如何访问数据框的(多)索引?

Pyspark - 一次聚合数据框的所有列[重复]

创建由 Groupby 和转换产生的数据框列