Python CSV文件 - 想要过滤特定的行/列

时间:2021-12-27 18:13:24

I'm new to Python and I'm trying to analyse this CSV file. It has a lot of different countries (as an example below).

我是Python的新手,我正在尝试分析这个CSV文件。它有很多不同的国家(以下为例)。

country iso2    iso3    iso_numeric g_whoregion year    e_pop_num   e_inc_100k  e_inc_100k_lo
Afghanistan AF  AFG 4   EMR 2000    20093756    190 123
American Samoa  AS  ASM 16  WPR 2003    59117   5.8 5   6.7 3   3   4
Gambia  GM  GMB 270 AFR 2010    1692149 178 115 254 3000    1900    4300

I want to try and obtain only specific data, so only specific countries and only specific columns (like "e_pop_numb"). How would I go about doing that?

我想尝试只获取特定数据,因此只有特定国家/地区和特定列(例如“e_pop_numb”)。我该怎么做呢?

The only basic code I have is:

我唯一的基本代码是:

import csv
import itertools

f = csv.reader(open('TB_burden_countries_2018-03-06.csv'))

for row in itertools.islice(f, 0, 10):
    print (row)

Which just lets me choose specific rows I want, but not necessarily the country I want to look at, or the specific columns I want.

这只是让我选择我想要的特定行,但不一定是我想看的国家,或者我想要的特定列。

IF you can help me or provide me a guide so I can do my own learning, I'd very much appreciate that! Thank you.

如果你可以帮助我或给我一个指导,这样我就可以自己学习,我非常感谢!谢谢。

3 个解决方案

#1


0  

I recommend you to use pandas python library. Please follow the article as here below there is a snippet code to iluminate your thoughts.

我建议你使用pandas python库。请按照下面的文章,有一个片段代码来阐明您的想法。

import pandas as pd
df1=pd.read_csv("https://pythonhow.com/wp-content/uploads/2016/01/Income_data.csv")
df2.loc["Alaska":"Arkansas","2005":"2007"]

source of this information: https://pythonhow.com/accessing-dataframe-columns-rows-and-cells/

此信息的来源:https://pythonhow.com/accessing-dataframe-columns-rows-and-cells/

#2


0  

Pandas will probably be the easiest way. https://pandas.pydata.org/pandas-docs/stable/

熊猫可能是最简单的方法。 https://pandas.pydata.org/pandas-docs/stable/

To get it run

让它运行

pip install pandas

Then read the csv into a dataframe and filter it

然后将csv读入数据帧并对其进行过滤

import pandas as pd

df = pd.read_csv(‘TB_burden_countries_2018-03-06.csv’)
df = df[df[‘country’] == ‘Gambia’]
print(df)

#3


0  

with open('file') as f: fields = f.readline().split("\t") print fields

使用open('file')作为f:fields = f.readline()。split(“\ t”)打印字段

If you supply more details about what you want to see, the answer would differ.

如果您提供有关您想要查看的内容的更多详细信息,答案会有所不同。

#1


0  

I recommend you to use pandas python library. Please follow the article as here below there is a snippet code to iluminate your thoughts.

我建议你使用pandas python库。请按照下面的文章,有一个片段代码来阐明您的想法。

import pandas as pd
df1=pd.read_csv("https://pythonhow.com/wp-content/uploads/2016/01/Income_data.csv")
df2.loc["Alaska":"Arkansas","2005":"2007"]

source of this information: https://pythonhow.com/accessing-dataframe-columns-rows-and-cells/

此信息的来源:https://pythonhow.com/accessing-dataframe-columns-rows-and-cells/

#2


0  

Pandas will probably be the easiest way. https://pandas.pydata.org/pandas-docs/stable/

熊猫可能是最简单的方法。 https://pandas.pydata.org/pandas-docs/stable/

To get it run

让它运行

pip install pandas

Then read the csv into a dataframe and filter it

然后将csv读入数据帧并对其进行过滤

import pandas as pd

df = pd.read_csv(‘TB_burden_countries_2018-03-06.csv’)
df = df[df[‘country’] == ‘Gambia’]
print(df)

#3


0  

with open('file') as f: fields = f.readline().split("\t") print fields

使用open('file')作为f:fields = f.readline()。split(“\ t”)打印字段

If you supply more details about what you want to see, the answer would differ.

如果您提供有关您想要查看的内容的更多详细信息,答案会有所不同。