Pandas Rolling Python 创建新列 [英] Pandas Rolling Python to create new Columns
问题描述
我有一个来自 excel 文件的数据框,其中包含如下数据:
I have a dataframe from excel file with data something like this:
A B Sum A Sum B
0
1 235353.21333333332
2 89160.59999999999
3 188382.98666666663
4 104677.1466666667
5 207723.25333333333
6 170128.02666666667
7 165287.5
8 44863.200000000004
9 177096.72
10 97687.71666666666 655447.7167 824912.6467
11 113207.76333333334 533302.2667 824912.6467
12 195151.2 444141.6667 1020063.847
13 151408.4433333333 255758.68 1171472.29
14 50865.66999999999 255758.68 1117660.813
15 84536.19000000002 255758.68 994473.75
16 217555.28666666665 473313.9667 824345.7233
17 90395.21666666666 563709.1833 659058.2233
18 126856.21 645702.1933 659058.2233
19 125190.61999999998 770892.8133 481961.5033
在 excel 文件中,要在第 10 行获得 Sum A,它使用 =SUM(A2:A12)
并在第 10 行获得 Sum B,它使用 =SUM(B2:B11)代码>.现在,我正在尝试使用
.rolling
重新创建它,但它会导致 DataError: No numeric types of aggregate.以下是我重新创建 Sum A 和 Sum B 的代码:
In excel file, to get Sum A at row 10 it uses =SUM(A2:A12)
and to get Sum B at row 10 it uses =SUM(B2:B11)
. Now, I am trying to recreate it by using .rolling
but it cause DataError: No numeric types of aggregate. The following is my code to recreate Sum A and Sum B:
df['Sum A'] = df['A'].rolling(10).sum()
df['Sum C'] = df['B'].rolling(10).sum()
有什么建议我应该怎么做才能重新创建 Sum A 和 Sum B 中的值?提前致谢
Any suggestion how should I do it in order to recreate the value in Sum A and Sum B? Thank you in advance
推荐答案
你可以试试吗:
df['Sum A'] = pd.to_numeric(df['A'], errors='coerce').fillna(0).rolling(10).sum()
这篇关于Pandas Rolling Python 创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!