Pandas:如何从 CSV 文件中读取特定行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55285217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas: How to read specific rows from a CSV file
提问by kev
I have a csv file example.csv
like-
我有一个 csv 文件,例如example.csv
-
name | hits
---------------
A | 34
B | 30
C | 25
D | 20
Using pandas
in Python, how do I only read the rows with hits > 20
? Looking for something like-
pandas
在 Python 中使用,我如何只读取行hits > 20
?寻找类似的东西-
my_df = pd.read_csv('example.csv', where col('hits') > 20)
my_df = pd.read_csv('example.csv', where col('hits') > 20)
回答by Sociopath
Read the entire csv and do filtering like below
阅读整个 csv 并进行如下过滤
my_df = pd.read_csv("example.csv")
my_df = my_df[my_df['hits']>20]
If you are having memory issues while reading, you can set chunksize
parameter to read it in chunks
如果您在阅读时遇到内存问题,您可以设置chunksize
参数以分块读取
回答by Loochie
Read the entire csv and then use query() method to select the required section :
阅读整个 csv,然后使用 query() 方法选择所需的部分:
required_df = my_df.query("hits > 20")
or,
或者,
required_df =df.loc[df['hits']>20]
回答by WMG
Once you create a dataframe from any source, you can simply use
从任何来源创建数据框后,您只需使用
dataframe_name['column_name'] (conditions) (value)
dataframe_name['column_name'](条件)(值)
something like
就像是
dataframe['score'] > 200