gpt4 book ai didi

printing - python pandas 打印数据框的元素

转载 作者:行者123 更新时间:2023-12-02 04:41:22 24 4
gpt4 key购买 nike

我有一个名为 country_codes 的 Pandas 数据框:

>>> country_codes.head(3)

COUNTRY FIPS ISO2 ISO3

0 Afghanistan AF AF AFG

1 Albania AL AL ALB

2 Algeria AG DZ DZA

给定特定的 fips 代码:

>>> fips = 'RS'

我选择与该 fips 代码对应的国家/地区名称:

>>> country = country_codes[country_codes['FIPS']==fips]['COUNTRY']

并打印出来:

>>> print(country)

201 Russia

Name: COUNTRY, dtype: object

我想在 matplotlib 图的标题中使用那个国家名称。我只想要国家名称。我不想要索引号或显示 Name: COUNTRY, dtype: object 的行。如何只获取名称?

最佳答案

你得到一个 series from indexing the dataframe

>>> country = country_codes[country_codes['FIPS']==fips]['COUNTRY']
>>> type(country)
<class 'pandas.core.series.Series'>

对于系列,selection by position :

>>> country.iloc[0]
'Russia'

关于printing - python pandas 打印数据框的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20766278/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com