问题
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
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()
Any suggestion how should I do it in order to recreate the value in Sum A and Sum B? Thank you in advance
回答1:
Can you try:
df['Sum A'] = pd.to_numeric(df['A'], errors='coerce').fillna(0).rolling(10).sum()
来源:https://stackoverflow.com/questions/64019861/pandas-rolling-python-to-create-new-columns