pandas 使用熊猫读取csv中的特定单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31813994/
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
reading specific cell in csv using pandas
提问by Uttara
I have a CSV file that looks like this:
我有一个如下所示的 CSV 文件:
patient_id, age_in_years, CENSUS_REGION, URBAN_RURAL_STATUS
11511, 7 Northeast, Urban,
9882613, 73, South, Urban,
32190339, 49, West, Urban,
32190339, 49, West, Urban,
32190339, 49, West, Urban,
32190339, 49, West, Urban,
.....
Right now my code looks like this:
现在我的代码是这样的:
df = pd.read_csv(filename, index_col = 0)
which gives the following output:
这给出了以下输出:
patient_id age_in_years CENSUS_REGION URBAN_RURAL_STATUS YEAR MONTH
11511 7 Northeast Urban 2011 6
9882613 73 South Urban 2011 7
32190339 49 West Urban 2011 8
32190339 49 West Urban 2011 8
32190339 49 West Urban 2011 8
32190339 49 West Urban 2011 8
32190339 49 West Urban 2011 8
32190339 49 West Urban 2011 8
...
I can get a specific column, e.g. CENSUS_REGION, by
我可以通过以下方式获取特定的列,例如 CENSUS_REGION
print(df['CENSUS_REGION'])
but I want to grab specific cells in the CSV. Can anyone please help me with this?
但我想获取 CSV 中的特定单元格。任何人都可以帮我解决这个问题吗?
采纳答案by Anand S Kumar
After getting the column, you can subscript using the indexto get the specific value for that cell.
获得后column,您可以使用下标index获取该单元格的特定值。
Example , in your case, your first column seems to be patient_id, so that is the index, you can index using that.
例如,在您的情况下,您的第一列似乎是patient_id,因此这是索引,您可以使用它进行索引。
Example -
例子 -
print(df['CENSUS_REGION'][11511])
The above would get the data of CENSUS_REGIONcolumn for the patient with id - 11511 .
以上将获得CENSUS_REGIONid - 11511 患者的列数据。
Example/Demo -
示例/演示 -
In [32]: df
Out[32]:
age_in_years CENSUS_REGION URBAN_RURAL_STATUS
patient_id
11511 7 Northeast Urban
9882613 73 South Urban
32190339 49 West Urban
32190339 49 West Urban
32190339 49 West Urban
32190339 49 West Urban
In [33]: df[' CENSUS_REGION']
Out[33]:
patient_id
11511 Northeast
9882613 South
32190339 West
32190339 West
32190339 West
32190339 West
Name: CENSUS_REGION, dtype: object
In [34]: df[' CENSUS_REGION'][11511]
Out[34]: ' Northeast'
Please note, I had to use lots of spaces, since the csv was messed up, but ' CENSUS_REGION'is just the column name.
请注意,我不得不使用很多空格,因为 csv 搞砸了,但' CENSUS_REGION'只是列名。

