基于模式和应用函数对列进行聚合的更泛的方法

2024-09-30 05:19:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道是否有更好的方法基于重复模式对列进行分组,然后应用函数。简单的版本是,我有几个月的数据,但希望它在季度。为了达到这一目标,我需要每3个月计算一次平均值()

    RegionName  State   1997-01 1997-02 1997-03 1997-04 1997-05 1997-06
1   Los Angeles CA      83      19      40      47      76      48
2   Chicago IL          39      87      48      3       71      18
3   Philadelphia    PA  60      85      8       46      81      48

期望的结果是:

    RegionName  State   1997q1      1997q2
1   Los Angeles CA      47.33333333 57
2   Chicago     IL          58          30.66666667
3   Philadelphia    PA  51          58.33333333

我使用python循环和一个预先制作的数字分步列表,有一种非常黑客的方法:

quartersbyendingdigit = {'1':'q1', '4':'q2', '7':'q3', '0':'q4'}
rl = list(range(2, housingdf.shape[1], 3))
for each in rl:
    og_column = housingdf.columns[each]
    new_column = og_column[:4] + quartersbyendingdigit[ og_column[-1] ]
    housingdf[new_column] = (housingdf[housingdf.columns[each]] + housingdf[housingdf.columns[each+1]] + housingdf[housingdf.columns[each+2]])/3

我不得不想象熊猫有一个更好的方法来做到这一点,因为这是一个非常明显的模式


Tags: columns方法模式columnilcaogstate
1条回答
网友
1楼 · 发布于 2024-09-30 05:19:13

您可以将datetime列转换为季度周期,然后用axis = 1将数据帧按列分组

df.set_index(["RegionName", "State"], inplace=True)    
df.columns = pd.to_datetime(df.columns).to_period("Q")    
df.groupby(level = 0, axis = 1).mean().reset_index()

enter image description here

相关问题 更多 >

    热门问题