如果我们有超过20列,如何对具有不同功能的数据帧进行重新采样?

我知道这个问题曾经被问过。答案如下:

df.resample('M').agg({'col1': np.sum,'col2': np.mean})

但是我有27列,我想对前25列求和,对其余两列求平均值。我应该为25列写this('col1'-'col25':np.sum)和为两列写this('col26':np.mean,'col27':np.mean)吗?

Mt数据框包含每小时数据,我想将其转换为每月数据。我想尝试类似的事情,但这是胡说八道:

for i in col_list:
    df = df.resample('M').agg({i-2: np.sum,'col26': np.mean,'col27': np.mean})

在这种情况下有捷径吗?

baihe703 回答:如果我们有超过20列,如何对具有不同功能的数据帧进行重新采样?

您可以尝试此操作,而不是for循环:

sum_col = ['col1','col2','col3','col4',...]
sum_df = df.resample('M')[sum_col].sum()

mean_col = ['col26','col27']
mean_df = df.resample('M')[mean_col].mean()

df = sum_col.join(mean_df)
本文链接:https://www.f2er.com/2885658.html

大家都在问