我的csv文件看起来像这样:
month,amount
jan,1
feb,3
mar,4
apr,7
may,9
我需要获取当前行-前一行,并从总和中获得平均值。如下所示:
month,amount
jan,1
feb,3 3-1 = 2
mar,4 4-3 = 1
apr,7 7-4 = 3
may,9 9-7 = 2
Average (2,1,3,2)
这是我到目前为止所拥有的,但是我得到了错误消息ValueError: Invalid Literal for Int() with base 10
。
with open("budget_data2.csv", "r") as f:
lines = f.readlines()
output = []
#header = lines[4]
data = [int(number) for number in lines[1:]]
for index, number in enumerate(data[1:],1):
output.append(number-data[index-1])
print(output)
print("Average: {}".format(sum(output)/len(output)))
发布于 2019-12-27 07:10:39
您需要在逗号处拆分每一行。您可以使用csv
模块自动解析CSV文件。
import csv
with open("budget_data2.csv", "r") as f:
rows = csv.DictReader(f)
data = [int(row['amount']) for row in rows]
发布于 2019-12-27 07:15:02
您可以像这样使用pandas (pip install pandas):
import pandas as pd
from io import StringIO
data = """
month,amount
jan,1
feb,3
mar,4
apr,7
may, 9
"""
df = pd.read_csv(StringIO(data),sep=',')
# dif
df['dif']=df[['amount']].diff()
print(df)
# average
print(df["dif"].mean())
结果:
month amount dif
0 jan 1 NaN
1 feb 3 2.0
2 mar 4 1.0
3 apr 7 3.0
4 may 9 2.0
2.0
发布于 2019-12-27 07:31:52
首先,考虑其他答案来提高您的Python编码技能。但是,如果您希望继续之前的操作,请去掉\n
字符(换行符),并使用逗号,
作为分隔符来分隔行。使用split
函数时,当您使用逗号拆分时,它会返回字符串的“部分”列表。
然后访问干净的数据。在代码中:
with open("budget_data2.csv", "r") as f:
lines = f.readlines()
data = []
for line in lines[1:]: #don't read the first line containing month and amount
line = line.strip("\n") #erase the newline character from each line
data.append(line.split(",")) #split the lines using the comma as a delimiter
output = []
for index,element in enumerate(data[1:]): #we start in the second element because we will subtract the first one
output.append(int(element[1])-int(data[index][1]))
average = sum(output)/len(output)
print(output)
print(average)
结果:
[2, 1, 3, 2]
2.0
https://stackoverflow.com/questions/59494280
复制相似问题