📌  相关文章
📜  从 Pandas 数据框中的列中获取唯一值

📅  最后修改于: 2022-05-13 01:55:24.401000             🧑  作者: Mango

从 Pandas 数据框中的列中获取唯一值

让我们看看如何从 pandas 数据框中检索唯一值。

让我们从 CSV 文件创建一个数据框。我们使用的是不同国家过去的 GDP 数据。您可以从这里获取数据集。

# import pandas as pd
import pandas as pd
  
gapminder_csv_url ='http://bit.ly/2cLzoxH'
# load the data with pd.read_csv
record = pd.read_csv(gapminder_csv_url)
  
record.head()

方法#1:从记录中选择列并应用唯一函数来获取我们想要的值。

# import pandas as pd
import pandas as pd
  
gapminder_csv_url ='http://bit.ly/2cLzoxH'
# load the data with pd.read_csv
record = pd.read_csv(gapminder_csv_url)
  
print(record['continent'].unique())

输出:

['Asia' 'Europe' 'Africa' 'Americas' 'Oceania']

方法#2:国家列中选择唯一值。

# import pandas as pd
import pandas as pd
  
gapminder_csv_url ='http://bit.ly/2cLzoxH'
  
# load the data with pd.read_csv
record = pd.read_csv(gapminder_csv_url)
  
print(record.country.unique())

输出:

['Afghanistan' 'Albania' 'Algeria' 'Angola' 'Argentina' 'Australia'
 'Austria' 'Bahrain' 'Bangladesh' 'Belgium' 'Benin' 'Bolivia'
 'Bosnia and Herzegovina' 'Botswana' 'Brazil' 'Bulgaria' 'Burkina Faso'
 'Burundi' 'Cambodia' 'Cameroon' 'Canada' 'Central African Republic'
 'Chad' 'Chile' 'China' 'Colombia' 'Comoros' 'Congo Dem. Rep.'
 'Congo Rep.' 'Costa Rica' "Cote d'Ivoire" 'Croatia' 'Cuba'
 'Czech Republic' 'Denmark' 'Djibouti' 'Dominican Republic' 'Ecuador'
 'Egypt' 'El Salvador' 'Equatorial Guinea' 'Eritrea' 'Ethiopia' 'Finland'
 'France' 'Gabon' 'Gambia' 'Germany' 'Ghana' 'Greece' 'Guatemala' 'Guinea'
 'Guinea-Bissau' 'Haiti' 'Honduras' 'Hong Kong China' 'Hungary' 'Iceland'
 'India' 'Indonesia' 'Iran' 'Iraq' 'Ireland' 'Israel' 'Italy' 'Jamaica'
 'Japan' 'Jordan' 'Kenya' 'Korea Dem. Rep.' 'Korea Rep.' 'Kuwait'
 'Lebanon' 'Lesotho' 'Liberia' 'Libya' 'Madagascar' 'Malawi' 'Malaysia'
 'Mali' 'Mauritania' 'Mauritius' 'Mexico' 'Mongolia' 'Montenegro'
 'Morocco' 'Mozambique' 'Myanmar' 'Namibia' 'Nepal' 'Netherlands'
 'New Zealand' 'Nicaragua' 'Niger' 'Nigeria' 'Norway' 'Oman' 'Pakistan'
 'Panama' 'Paraguay' 'Peru' 'Philippines' 'Poland' 'Portugal'
 'Puerto Rico' 'Reunion' 'Romania' 'Rwanda' 'Sao Tome and Principe'
 'Saudi Arabia' 'Senegal' 'Serbia' 'Sierra Leone' 'Singapore'
 'Slovak Republic' 'Slovenia' 'Somalia' 'South Africa' 'Spain' 'Sri Lanka'
 'Sudan' 'Swaziland' 'Sweden' 'Switzerland' 'Syria' 'Taiwan' 'Tanzania'
 'Thailand' 'Togo' 'Trinidad and Tobago' 'Tunisia' 'Turkey' 'Uganda'
 'United Kingdom' 'United States' 'Uruguay' 'Venezuela' 'Vietnam'
 'West Bank and Gaza' 'Yemen Rep.' 'Zambia' 'Zimbabwe']

方法#3:

在这种方法中,您可以看到我们使用 unique函数中的数据框作为参数,尽管我们选择与上面相同的列,所以我们得到相同的输出。

# Write Python3 code here
# import pandas as pd
import pandas as pd
  
gapminder_csv_url ='http://bit.ly/2cLzoxH'
  
# load the data with pd.read_csv
record = pd.read_csv(gapminder_csv_url)
  
print(pd.unique(record['continent']))

输出:

['Asia' 'Europe' 'Africa' 'Americas' 'Oceania']