![](/img/trans.png)
[英]pandas dataframe check if column contains string that exists in another column
[英]Pandas: Check if dataframe column exists in the json object
我有一个名为'countries'的json对象,如下所示,所有国家的ISO代码列表:
countries = [{"name":"Afghanistan","alpha-2":"AF","country-code":"004"},{"name":"Åland Islands","alpha-2":"AX","country-code":"248"},{"name":"Albania","alpha-2":"AL","country-code":"008"},{"name":"Algeria","alpha-2":"DZ","country-code":"012"}]
我有一个带有'Country'列的pandas数据框:
Country
--------
AU
AL
DZ
如何检查“国家/地区”列中的任何行是否存在于json对象的“alpha-2”列中,如果不存在则打印错误?
当我尝试下面的代码时,我没有得到任何错误,也没有打印任何东西。
if df['Country'].any() in [x['alpha-2'] for x in countries]:
print "Country code exists"
你可以做到
if set(x['alpha-2'] for x in countries).intersection(df.Country):
print('Country code exists')
或者,在精神上更接近你所尝试的(但具有完全不同的性能特征),
if df.Country.isin(x['alpha-2'] for x in countries).any():
print('Country code exists')
由于您已经有一个pandas DataFrame,您可以将JSON对象转换为DataFrame,使用pd.merge执行两者的inner
联接 ,然后检查返回的DataFrame是否为空。
>>> import pandas as pd
>>> countries_base = [{'Country': 'AU'}, {'Country': 'AL'}, {'Country': 'DZ'}]
>>> countries = [{"name":"Afghanistan","alpha-2":"AF","country-code":"004"},{"name":"Åland Islands","alpha-2":"AX","country-code":"248"},{"name":"Albania","alpha-2":"AL","country-code":"008"},{"name":"Algeria","alpha-2":"DZ","country-code":"012"}]
>>> df1 = pd.DataFrame(countries_base)
>>> df2 = pd.DataFrame(countries)
>>> m = pd.merge(df1, df2, how='inner', left_on='Country', right_on='alpha-2')
>>> if m.empty:
>>> print('Country code does not exist')
>>> else:
>>> print('Country code exists')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.