[英]Sum of a column in python for CSV file
Using pandas
module it might look like (more or less) 使用
pandas
模块可能看起来像(或多或少)
import pandas as pd
df = pd.read_csv(filename, ... some other options ...)
print df[2].sum()
# print df['distance'].sum()
Here is an approach using the builtin csv module 这是使用内置csv模块的方法
import csv
csv_file = csv.reader(open("your_file_name.csv"))
dist = 0
for row in csv_file:
_dist = row[2]
try:
_dist = float(_dist)
except ValueError:
_dist = 0
dist += _dist
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.