首页 > 解决方案 > 如何在 CSV 文件中查找特定列的累积总和

问题描述

我有一个格式为 csv 的文件:

20 05 2019 12:00:00, 100

21 05 2019 12:00:00, 200

22 05 2019 12:00:00, 480

我想访问第二个变量,我尝试了各种不同的更改,但都没有奏效。最初我尝试过

import pandas as pd
import numpy as np

col = [i for i in range(2)]
col[1] = "Power"
data = pd.read_csv('FILENAME.csv', names=col)


df1 = data.sum(data, axis=1)
df2 = np.cumsum(df1)
print(df2)

标签: pythonpandasnumpy

解决方案


使用df.cumsum

In [1820]: df = pd.read_csv('FILENAME.csv', names=col)    
In [1821]: df
Out[1821]: 
                     0  Power
0  20 05 2019 12:00:00    100
1  21 05 2019 12:00:00    200
2  22 05 2019 12:00:00    480

In [1823]: df['cumulative sum'] = df['Power'].cumsum()    
In [1824]: df    
Out[1824]: 
                     0  Power  cumulative sum
0  20 05 2019 12:00:00    100             100
1  21 05 2019 12:00:00    200             300
2  22 05 2019 12:00:00    480             780

推荐阅读