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

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

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

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})

这种情况下有捷径吗?

dataframe resampling
1个回答
0
投票

您可以尝试此操作,而不要用于循环:

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)
© www.soinside.com 2019 - 2024. All rights reserved.