简体   繁体   English

如何使用Python在大型csv文件上打印特定年份

[英]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. 所以我知道f_set将打印出我的y单元格与2005年的相应年份。 What if I want to print out the years 2002 through 2009? 如果我想打印出2002年至2009年的内容怎么办?

It seems you need add second condition to boolean indexing : 看来你需要为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 : 默认情况下, between另一个解决方案包括

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

consider the pd.DataFrame f 考虑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]

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中打印csv文件中的特定列? - How to print a specific a column in a csv file in python? 如何在python中未命名为csv的csv文件中打印特定行? - How to print a specific line in a csv file that is not named csv in python? 如何根据用户输入在python中打印csv文件的特定行? - how to print a specific line of a csv file in python based on user input? 如何使用 python 打印 a.csv 文件两列的值 - How to print values for two columns of a .csv file using python 如何查询我已输入的csv文件的特定列并使用python打印所有返回的行? - How can i query a specific column of a csv file that i have input and print all returned rows using python? 如何在 Python 上大量计算特定数字(未列出) - How to count specific numbers in massive on Python (not list) 在python中使用CSV文件打印列 - Using CSV file in python to print column Python - 查找 CSV 中的列的平均值,给定另一列中的值(来自具有多年的文件中特定年份的数据)? - Python - Finding average of a column in a CSV given a value in another column (data from a specific year in a file with multiple years)? 如何使用python连续在特定的时间间隔内逐行打印文件? - How to print a file line by line in specific interval continuously using python? 如何使用python读取csv文件的特定列 - How to read a specific column of csv file using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM