需要您的帮助,了解如何计算每行每4列的平均值,并将平均值输出到numpy数组。
df = pd.read_excel (open(excel_path,'rb'), skiprows= 5, skipfooter= 27)
我正在使用pandas readexcel导入数据帧中的excel数据,数据帧如下所示:
Variable 2003 4Q 2004 1Q 2004 2Q 2004 3Q 2004 4Q ....... 2020 1Q
0 A unwanted_value1 913614 921129 924066 942764
1 B unwanted_value2 49757 51065.7 52029.2 51213.1
2 C unwanted_value3 19255.4 19152.5 18857 16825
3 D unwanted_value4 na na na na
4 E unwanted_value5 25092.1 26505.2 27760 28604.6
5 F unwanted_value6 863857 870063 872037 891551
正如你所看到的,有两个问题:
数据的某些部分不完整(例如,2003年只有第4季度的结果(,我需要排除它们。
有";na";某行中的值。
我很难将.mean和.iloc 结合起来
df.meal(轴=0,skipna=True,级别=无,numeric_only=无(
我尝试使用计数器来跟踪每4个结果,并使用column_index来指示要开始计算的列。
例如,我将值3分配给column_index,我认为这将使计算从2004 1Q开始,然后将df.iloc[column_index]馈送到df.mean方法。然而,对于如何将它们结合在一起,我有点困惑。
此外,该方法必须考虑一种在特定列索引处停止的方式。
counter = 0 # counts every 4 result and reset again.
column_index = 3
while True:
if counter != 4:
#code need to calculate the average of the quarterly result using column index and output to numpy array.
column_index += 4
counter += counter
elif counter == 4:
counter = 0
return()
将用于进一步处理的所需numpy数组:
[average_for_2004 average_for_2005 average_for_2006 average_for_2007 .....]
请浏览到网站的末尾并点击";新加坡外债统计,(期末(,季度";链接到导出excel文件的网站(需要选择导出为.xlsx并临时禁用弹出窗口阻止程序以显示下载对话框(:https://www.singstat.gov.sg/find-data/search-by-theme/economy/external-debt/latest-data
首先分离要聚合的列(所有列从2004 1Q开始(:
df2 = df.loc[:, '2004 1Q':]
然后生成分组字典:
grpDct = { t: t.split(' ')[0] for t in df2.columns }
它将把连续4列的范围映射到组。
实际计算包括:
- 按上述字典分组(水平(
- 每个组的计算装置
- 将df的第一列与上述分组连接起来
这样做的代码是:
result = df[['Variable']].join(df2.groupby(grpDct, axis=1).mean())
对于我的样本数据,仅限于2003年、2004年和2005年,结果是:
Variable 2004 2005
0 A 925393.250 572893.250
1 B 54266.250 36841.250
2 C 18522.475 11722.475
3 D NaN 325.000
4 E 26990.475 12840.475
5 F 874377.000 466827.000
或者只使用分组,而不与Variable列连接(以及转换为Numpy数组(:
resNp = df2.groupby(grpDct, axis=1).mean().values
这次的结果是:
array([[9.2539325e+05, 5.7289325e+05],
[5.4266250e+04, 3.6841250e+04],
[1.8522475e+04, 1.1722475e+04],
[ nan, 3.2500000e+02],
[2.6990475e+04, 1.2840475e+04],
[8.7437700e+05, 4.6682700e+05]])
编辑
我注意到你希望每年的结果都是";合计";意思是而不是每一行(每年(的平均值。
要获得这样的结果,您可以仅依靠Numpy函数:
# Separate the "wanted" columns
a1 = df.iloc[:, 2:].values
# Create a list of "sections" for each year
a2 = np.array_split(a1, a1.shape[1] // 4, axis=1)
# Compute mean for each "section"
resNp = np.array([np.nanmean(a) for a in a2])
对于我得到的样本数据(2004年和2005年的平均值(:
array([379909.89 , 200233.99090909])
试试这个:
valid_years = [y.split("_")[0] for y in df.columns if y.endswith("_Q1") | y.endswith("_Q2") | y.endswith("_Q3") | y.endswith("_Q4")]
valid_years = [x for x in set(valid_years) if valid_years.count(x)==4]
results = {}
for year in valid_years:
results[year] = df.filter(regex='^'+str(year), axis=1).unstack().astype(float).mean(skipna=True)
print(results)