Pandas: Check if dataframe column exists in the json object
up vote
3
down vote
favorite
I have a json object called 'countries' like below with all the countries ISO code list:
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"]
I have a pandas dataframe with 'Country' column:
Country
--------
AU
AL
DZ
How can I check if any row in 'Country' column exists in 'alpha-2' column of the json object and print error if it does not exist?
When I try the below code, I don't get any error nor does it print anything.
if df['Country'].any() in [x['alpha-2'] for x in countries]:
print "Country code exists"
python pandas dataframe
add a comment |
up vote
3
down vote
favorite
I have a json object called 'countries' like below with all the countries ISO code list:
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"]
I have a pandas dataframe with 'Country' column:
Country
--------
AU
AL
DZ
How can I check if any row in 'Country' column exists in 'alpha-2' column of the json object and print error if it does not exist?
When I try the below code, I don't get any error nor does it print anything.
if df['Country'].any() in [x['alpha-2'] for x in countries]:
print "Country code exists"
python pandas dataframe
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I have a json object called 'countries' like below with all the countries ISO code list:
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"]
I have a pandas dataframe with 'Country' column:
Country
--------
AU
AL
DZ
How can I check if any row in 'Country' column exists in 'alpha-2' column of the json object and print error if it does not exist?
When I try the below code, I don't get any error nor does it print anything.
if df['Country'].any() in [x['alpha-2'] for x in countries]:
print "Country code exists"
python pandas dataframe
I have a json object called 'countries' like below with all the countries ISO code list:
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"]
I have a pandas dataframe with 'Country' column:
Country
--------
AU
AL
DZ
How can I check if any row in 'Country' column exists in 'alpha-2' column of the json object and print error if it does not exist?
When I try the below code, I don't get any error nor does it print anything.
if df['Country'].any() in [x['alpha-2'] for x in countries]:
print "Country code exists"
python pandas dataframe
python pandas dataframe
edited Nov 9 at 20:20
fuglede
6,34211238
6,34211238
asked Nov 9 at 19:47
Raj
9210
9210
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
You could do
if set(x['alpha-2'] for x in countries).intersection(df.Country):
print('Country code exists')
or, closer in spirit to what you are trying (but with completely different performance characteristics),
if df.Country.isin(x['alpha-2'] for x in countries).any():
print('Country code exists')
Thanks @fuglede your answer helped me.
– Raj
Nov 9 at 20:03
add a comment |
up vote
3
down vote
Since you already have a pandas DataFrame, you could convert the JSON object into a DataFrame, do an inner join of both using pd.merge, and then check if the returned DataFrame is empty or not.
>>> 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')
Thanks @Vinayak. I wanted to achieve this without creating a df for the countries object.
– Raj
Nov 9 at 20:04
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You could do
if set(x['alpha-2'] for x in countries).intersection(df.Country):
print('Country code exists')
or, closer in spirit to what you are trying (but with completely different performance characteristics),
if df.Country.isin(x['alpha-2'] for x in countries).any():
print('Country code exists')
Thanks @fuglede your answer helped me.
– Raj
Nov 9 at 20:03
add a comment |
up vote
4
down vote
accepted
You could do
if set(x['alpha-2'] for x in countries).intersection(df.Country):
print('Country code exists')
or, closer in spirit to what you are trying (but with completely different performance characteristics),
if df.Country.isin(x['alpha-2'] for x in countries).any():
print('Country code exists')
Thanks @fuglede your answer helped me.
– Raj
Nov 9 at 20:03
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You could do
if set(x['alpha-2'] for x in countries).intersection(df.Country):
print('Country code exists')
or, closer in spirit to what you are trying (but with completely different performance characteristics),
if df.Country.isin(x['alpha-2'] for x in countries).any():
print('Country code exists')
You could do
if set(x['alpha-2'] for x in countries).intersection(df.Country):
print('Country code exists')
or, closer in spirit to what you are trying (but with completely different performance characteristics),
if df.Country.isin(x['alpha-2'] for x in countries).any():
print('Country code exists')
edited Nov 10 at 18:39
answered Nov 9 at 19:51
fuglede
6,34211238
6,34211238
Thanks @fuglede your answer helped me.
– Raj
Nov 9 at 20:03
add a comment |
Thanks @fuglede your answer helped me.
– Raj
Nov 9 at 20:03
Thanks @fuglede your answer helped me.
– Raj
Nov 9 at 20:03
Thanks @fuglede your answer helped me.
– Raj
Nov 9 at 20:03
add a comment |
up vote
3
down vote
Since you already have a pandas DataFrame, you could convert the JSON object into a DataFrame, do an inner join of both using pd.merge, and then check if the returned DataFrame is empty or not.
>>> 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')
Thanks @Vinayak. I wanted to achieve this without creating a df for the countries object.
– Raj
Nov 9 at 20:04
add a comment |
up vote
3
down vote
Since you already have a pandas DataFrame, you could convert the JSON object into a DataFrame, do an inner join of both using pd.merge, and then check if the returned DataFrame is empty or not.
>>> 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')
Thanks @Vinayak. I wanted to achieve this without creating a df for the countries object.
– Raj
Nov 9 at 20:04
add a comment |
up vote
3
down vote
up vote
3
down vote
Since you already have a pandas DataFrame, you could convert the JSON object into a DataFrame, do an inner join of both using pd.merge, and then check if the returned DataFrame is empty or not.
>>> 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')
Since you already have a pandas DataFrame, you could convert the JSON object into a DataFrame, do an inner join of both using pd.merge, and then check if the returned DataFrame is empty or not.
>>> 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')
answered Nov 9 at 19:57
Vinayak Mehta
13110
13110
Thanks @Vinayak. I wanted to achieve this without creating a df for the countries object.
– Raj
Nov 9 at 20:04
add a comment |
Thanks @Vinayak. I wanted to achieve this without creating a df for the countries object.
– Raj
Nov 9 at 20:04
Thanks @Vinayak. I wanted to achieve this without creating a df for the countries object.
– Raj
Nov 9 at 20:04
Thanks @Vinayak. I wanted to achieve this without creating a df for the countries object.
– Raj
Nov 9 at 20:04
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232374%2fpandas-check-if-dataframe-column-exists-in-the-json-object%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown