Pandas: Check if dataframe column exists in the json object









up vote
3
down vote

favorite
1












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"









share|improve this question



























    up vote
    3
    down vote

    favorite
    1












    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"









    share|improve this question

























      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      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"









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 20:20









      fuglede

      6,34211238




      6,34211238










      asked Nov 9 at 19:47









      Raj

      9210




      9210






















          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')





          share|improve this answer






















          • Thanks @fuglede your answer helped me.
            – Raj
            Nov 9 at 20:03

















          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')





          share|improve this answer




















          • Thanks @Vinayak. I wanted to achieve this without creating a df for the countries object.
            – Raj
            Nov 9 at 20:04










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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

























          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')





          share|improve this answer






















          • Thanks @fuglede your answer helped me.
            – Raj
            Nov 9 at 20:03














          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')





          share|improve this answer






















          • Thanks @fuglede your answer helped me.
            – Raj
            Nov 9 at 20:03












          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')





          share|improve this answer














          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')






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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
















          • 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












          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')





          share|improve this answer




















          • Thanks @Vinayak. I wanted to achieve this without creating a df for the countries object.
            – Raj
            Nov 9 at 20:04














          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')





          share|improve this answer




















          • Thanks @Vinayak. I wanted to achieve this without creating a df for the countries object.
            – Raj
            Nov 9 at 20:04












          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')





          share|improve this answer












          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')






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
















          • 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

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Kleinkühnau

          Makov (Slowakei)

          Deutsches Schauspielhaus