简体   繁体   中英

How to print specific years on a massive csv file using Python

import pandas

fileref = open('chart.csv')
f = pandas.read_csv(fileref)

f_set = f[f.year >= 2005]
print(f_set.groupby('y').namefromchart.nunique())

So I know f_set will print out my y cells with the corresponding years of 2005 through current. What if I want to print out the years 2002 through 2009?

It seems you need add second condition to boolean indexing :

import pandas as pd

f = pd.read_csv('chart.csv')
f_set = f[(f.year >= 2002) & (f.year < 2010)]
print (f_set.groupby('y').namefromchart.nunique())

Another solution with between , by default is inclusive :

f_set = f[f.year.between(2002,2009)]

consider the pd.DataFrame f

f = pd.DataFrame(dict(year=range(2000, 2011), A=np.random.rand(11)))

you can set the index and slice how you'd like

f.set_index('year').ix[2002:2009]

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM