Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python从csv中提取信息_Python_Csv - Fatal编程技术网

使用python从csv中提取信息

使用python从csv中提取信息,python,csv,Python,Csv,我试图从csv中提取每年发布多少首歌曲。我的数据是这样的 no,artist,name,year "1","Bing Crosby","White Christmas","1942" "2","Bill Haley & his Comets","Rock Around the Clock","1955" "3","Sinead O'Connor","Nothing Compares 2 U","1990","35.554" "4","Celine Dion","My Heart Will

我试图从csv中提取每年发布多少首歌曲。我的数据是这样的

no,artist,name,year
"1","Bing Crosby","White Christmas","1942"
"2","Bill Haley & his Comets","Rock Around the Clock","1955"
"3","Sinead O'Connor","Nothing Compares 2 U","1990","35.554"
"4","Celine Dion","My Heart Will Go On","1998","35.405"
"5","Bryan Adams","(Everything I Do) I Do it For You","1991"
"6","The Beatles","Hey Jude","1968"
"7","Whitney Houston","I Will Always Love You","1992","34.560"
"8","Pink Floyd","Another Brick in the Wall (part 2)","1980"
"9","Irene Cara","Flashdance... What a Feeling","1983"
"10","Elton John","Candle in the Wind '97","1992"
我的文件由3000行数据和附加字段组成,但我有兴趣提取每年发布多少首歌曲

我试图提取年份和歌曲,我的代码在这里,但我是python新手,因此我不知道如何解决我的问题。我的代码是

from itertools import islice
import csv


filename = '/home/rob/traintask/top3000songs.csv'
data = csv.reader(open(filename))
# Read the column names from the first line of the file
fields = data.next()[3]  // I tried to read the year columns
print fields
count = 0
for row in data:
    # Zip together the field names and values
    items = zip(fields, row)
    item = {}   \\ here I am lost, i think i should make a dict and set year as key and no of songs as values, but I don't know how to do it
    # Add the value to our dictionary
    for (name, value) in items:
        item[name] = value.strip()
        print 'item: ', item

我完全错了。但如果有人给我一些提示或帮助,我怎么能数不出一年内发行的歌曲。我会很感激的

2行非常简单的代码:

import pandas as pd
my_csv=pd.read_csv(filename)
要获得每年的歌曲数量:

songs_per_year= my_csv.groupby('year')['name'].count()

2行非常简单的代码:

import pandas as pd
my_csv=pd.read_csv(filename)
要获得每年的歌曲数量:

songs_per_year= my_csv.groupby('year')['name'].count()

您可以使用模块中的
计数器
对象


您可以使用模块中的
计数器
对象


谢谢你的回复。但我如何计算每年发行多少首歌曲呢。我知道我必须做一个循环,但我没有任何逻辑。其次,可以使用简单的csv而不是熊猫?my_csv.groupby('year')['names'].count()@rob BTW您的csv有问题,因为有些行有5个字段,而不是4个。我想,songs_per_year=my_csv.groupby('year')。size()更干净。@andre我部分同意,虽然在我看来,这是一个有点不可读,因为它不太清楚,我们正在计算的歌曲。谢谢你的答复。但我如何计算每年发行多少首歌曲呢。我知道我必须做一个循环,但我没有任何逻辑。其次,可以使用简单的csv而不是熊猫?my_csv.groupby('year')['names'].count()@rob BTW您的csv有问题,因为有些行有5个字段,而不是4个。我想,songs_per_year=my_csv.groupby('year')。size()更干净。@andre我部分同意,虽然在我看来,这是一个有点不可读,因为它不太清楚,我们正在计算的歌曲。thanx很多。“我会试试看,很快就会回来的。”非常感谢timgeb。您的解决方案也可以很好地工作,而且效果良好。我会接受他的回答,因为如果你先回答的话。但我真的很感谢你的帮助。谢谢你。“我会试试看,很快就会回来的。”非常感谢timgeb。您的解决方案也可以很好地工作,而且效果良好。我会接受他的回答,因为如果你先回答的话。但我真的很感谢你的帮助。