如何找到avg的csv文件列

时间:2016-01-28 08:14:10

标签: python python-2.7 python-3.x numpy

import csv
with open('Met.csv', 'r') as f:
    reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE)
    for row in reader:
        print row

我无法继续如何从我尝试的csv文件中获取列

print row[:column_name]

name    id  name    reccla  mass (g)    fall    year    GeoLocation

Aachen  1       Valid   L5   21     Fell    01/01/1880 (50.775000, 6.083330)    
Aarhus  2       Valid   H6  720     Fell    1/1/1951  (53.775000, 6.586560)
Abee    6       Valid   EH4 --      Fell    1/1/1952 (50.775000, 6.083330)  
Acapul  10      Valid   A   353      Fell   1/1/1952  (50.775000, 6.083330)
Acapul  1914    valid   A    --      Fell   1/1/1952 (50.775000, 6.083330)
AdhiK   379    Valid    EH4 56655    Fell   1/1/1919 (50.775000, 6.083330)

我希望平均质量(g)

2 个答案:

答案 0 :(得分:1)

尝试pandas而不是阅读csv

import pandas as pd
data = pd.read_csv('Met.csv')

使用pandas抓取列并执行操作要容易得多。 这里我将csv内容加载到数据帧。

已加载数据示例数据

>>> data
       name   id nametype     recclass    mass
0    Aarhus    2    Valid           H6     720
1      Abee    6    Valid          EH4  107000
2  Acapulco   10    Valid  Acapulcoite     914
3   Achiras  370    Valid           L6     780
4  Adhi Kot  379    Valid          EH4    4239
5     Adzhi  390    Valid        LL3-6     910
6      Agen  392    Valid           H5   30000

只是质量列:

您可以访问各个列作为数据['列名称']

>>> data['mass']
0       720
1    107000
2       914
3       780
4      4239
5       910
6     30000
Name: mass, dtype: int64

质量平均列:

>>> data['mass'].mean()
20651.857142857141

答案 1 :(得分:0)

您可以使用csv.DictReader()代替csv.reader()。以下代码对我来说很好用

import csv

mass_list = []
with open("../data/Met.csv", "r") as f:
    reader = csv.DictReader(f, delimiter="\t")
    for row in reader:
        mass = row["mass"]
        if mass is not None and mass is not "--":
            mass_list.append(float(row["mass"]))

avg_mass = sum(mass_list) / len(mass_list)
print "avg of mass: ", avg_mass

希望它有所帮助。