我是python新手,正在研究一个数据框架,它的第一列是"Country",后面是144列数字数据.

Goal and Expected Result:

需要创建平均为3列的新列.例:第一个新列是前三列数值数据(列号1、2和3)的平均值.下一个新列是后续3列(列编号4、5和6)的平均值,依此类推.由于此数据集有144列,我们需要创建48个新列(144/3).请在下面找到数据帧的快照

Snapshot of the Data

我正在使用下面的代码,这绝对不是一种理想的方法,应该有更好的方法.

是否有人可以建议是否可以使用循环函数实现输出?

df = pd.read_excel('/content/df_Data.xlsx')

df[2010_1] = df[[1,2,3]].mean(axis=1)
df[2010_2] = df[[4,5,6]].mean(axis=1)
df[2010_3] = df[[7,8,9]].mean(axis=1)
df[2010_4] = df[[10,11,12]].mean(axis=1)
df[2011_1] = df[[13,14,15]].mean(axis=1)
df[2011_2] = df[[16,17,18]].mean(axis=1)
df[2011_3] = df[[19,20,21]].mean(axis=1)
df[2011_4] = df[[22,23,24]].mean(axis=1)
df[2012_1] = df[[25,26,27]].mean(axis=1)
df[2012_2] = df[[28,29,30]].mean(axis=1)
df[2012_3] = df[[31,32,33]].mean(axis=1)
df[2012_4] = df[[34,35,36]].mean(axis=1)
df[2013_1] = df[[37,38,39]].mean(axis=1)
df[2013_2] = df[[40,41,42]].mean(axis=1)
df[2013_3] = df[[43,44,45]].mean(axis=1)
df[2013_4] = df[[46,47,48]].mean(axis=1)
df[2014_1] = df[[49,50,51]].mean(axis=1)
df[2014_2] = df[[52,53,54]].mean(axis=1)
df[2014_3] = df[[55,56,57]].mean(axis=1)
df[2014_4] = df[[58,59,60]].mean(axis=1)
df[2015_1] = df[[61,62,63]].mean(axis=1)
df[2015_2] = df[[64,65,66]].mean(axis=1)
df[2015_3] = df[[67,68,69]].mean(axis=1)
df[2015_4] = df[[70,71,72]].mean(axis=1)
df[2016_1] = df[[73,74,75]].mean(axis=1)
df[2016_2] = df[[76,77,78]].mean(axis=1)
df[2016_3] = df[[79,80,81]].mean(axis=1)
df[2016_4] = df[[82,83,84]].mean(axis=1)
df[2017_1] = df[[85,86,87]].mean(axis=1)
df[2017_2] = df[[88,89,90]].mean(axis=1)
df[2017_3] = df[[91,92,93]].mean(axis=1)
df[2017_4] = df[[94,95,96]].mean(axis=1)
df[2018_1] = df[[97,98,99]].mean(axis=1)
df[2018_2] = df[[100,101,102]].mean(axis=1)
df[2018_3] = df[[103,104,105]].mean(axis=1)
df[2018_4] = df[[106,107,108]].mean(axis=1)
df[2019_1] = df[[109,110,111]].mean(axis=1)
df[2019_2] = df[[112,113,114]].mean(axis=1)
df[2019_3] = df[[115,116,117]].mean(axis=1)
df[2019_4] = df[[118,119,120]].mean(axis=1)
df[2020_1] = df[[121,122,123]].mean(axis=1)
df[2020_2] = df[[124,125,126]].mean(axis=1)
df[2020_3] = df[[127,128,129]].mean(axis=1)
df[2020_4] = df[[130,131,132]].mean(axis=1)
df[2021_1] = df[[133,134,135]].mean(axis=1)
df[2021_2] = df[[136,137,138]].mean(axis=1)
df[2021_3] = df[[139,140,141]].mean(axis=1)
df[2021_4] = df[[142,143,144]].mean(axis=1)

推荐答案

比另一个答案要详细得多,但希望仍然有用.其 idea 是:(1)将"宽表"融化为"长表";(2)添加一个年/季度列,以及(3)在该列上分组.

import pandas as pd
import numpy as np

num_cols = 14
num_rows = 5

np.random.seed(1)

#Create a table in the same shape that you describe
#columns are ints which I'm guessing represent months
df = pd.DataFrame({
    c+1:np.random.randint(1,20,num_rows) for c in range(num_cols)
})

#Melt the table to "long form" where each row has the previous column name and value
long_df = df.melt(var_name='month',value_name='val')

#Add quarter_year column
years = long_df['month'].sub(1).floordiv(12).add(2010)
quarters = long_df['month'].sub(1).mod(12).floordiv(3).add(1) #better way to do this?
long_df['year_quarter'] = years.astype(str)+'_'+quarters.astype(str)

#Use groupby to get the mean value per year_quarter (you can reshape the table later if you need)
long_df.groupby('year_quarter')['val'].mean().reset_index(name='mean_quarter_values')

这是df号宽桌的样子

enter image description here

long_df

enter image description here

最终输出

enter image description here

Python相关问答推荐

替换字符串中的多个重叠子字符串

Python中的嵌套Ruby哈希

在Pandas DataFrame操作中用链接替换'方法的更有效方法

用Python解密Java加密文件

根据二元组列表在pandas中创建新列

使用setuptools pyproject.toml和自定义目录树构建PyPi包

运输问题分支定界法&

无法使用DBFS File API路径附加到CSV In Datricks(OSError Errno 95操作不支持)

如何设置视频语言时上传到YouTube与Python API客户端

python中字符串的条件替换

合并帧,但不按合并键排序

如何在TensorFlow中分类多个类

为什么在FastAPI中创建与数据库的连接时需要使用生成器?

需要帮助使用Python中的Google的People API更新联系人的多个字段'

当我定义一个继承的类时,我可以避免使用`metaclass=`吗?

为什么Visual Studio Code说我的代码在使用Pandas concat函数后无法访问?

在Pandas 中以十六进制显示/打印列?

为什么在生成时间序列时,元组索引会超出范围?

Django查询集-排除True值

使用Django标签显示信息