0

## python dataframe加總

index data1 sum1
0 2
1 4
2 5 11
3 6 15
4 7 18
5 8 21
6 9 24
7 10 27

for i in range(len(data)-passcount):
print(df.iloc[i+1:i+passcount+1,0].sum())

### 2 個回答

2

iT邦新手 3 級 ‧ 2018-12-09 18:53:55

sum[temp]=data[temp-2:temp]

pd.rolling_sum(s2['data'], 3)

``````import pandas as pd
import numpy as np

s2 = pd.DataFrame(np.random.randint(10,size=(8,1)),columns=["data"])
print(s2)
s2['mean_sum'] = pd.rolling_sum(s2['data'], 3)

print(s2)
``````

0

iT邦新手 5 級 ‧ 2018-12-09 20:21:52

``````import pandas as pd

datas = [2, 4, 5, 6, 7, 8, 9, 10]
column = ['data1']
df = pd.DataFrame(datas, columns=column)
df['sum1'] = None

sum_total = sum([df.loc[x, 'data1'] for x in range(3)])
sum1 = [sum_total]

for i in range(len(datas)-3):
sum_total += (df.iloc[i+3, 0] - df.iloc[i, 0])
sum1.append(sum_total)

# df['sum1'][2:] = sum1

df.loc[2:,'sum1'] = sum1

print(sum1)
print(df)

``````

ginbow iT邦新手 5 級 ‧ 2018-12-09 23:27:35 檢舉

``````import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df = pd.DataFrame([1,2,3,4,5], columns=['idx'])
df['price']=['nan',200,300,440,684]
df['price2']=['nan','nan',300,440,684]
df['price']=df['price'].astype('float64')
df['price2']=df['price2'].astype('float64')
df.set_index('idx', inplace=True)
df.plot(y='price2', use_index=True)

``````