-1

I have the following dataframe:

      import pandas as pd

      df = pd.DataFrame({'Name': ['MEXICO', 'CANADA', 'CANADA', 'PORTUGAL', 'ESPANHA', 
                                  'BRASIL', 'BRASIL', 'MEXICO'],                   
                         'Column_two': [1,2,3,4,5,6,7,8]                  
                         })

      print(df)

      # Output:

                  Name      Column_two
                 MEXICO        1
                 CANADA        2
                 CANADA        3
                PORTUGAL       4
                 ESPANHA       5
                  BRASIL       6
                  BRASIL       7
                  MEXICO       8

And I have the following list with some cities located in America:

      list_americ = ['MEXICO', 'CANADA', 'BRASIL']

I would like to keep in the dataframe only the 'Name' of the countries that are in the list_americ. So I tried to do the following code:

     for correct_name in list_americ:
          df['Name'] = df['Name'].apply(lambda x: correct_name if correct_name == df['Name'] 
                       else x)

This code is producing the following error:

      ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I would like the output to be:

                  Name      Column_two
                 MEXICO        1
                 CANADA        2
                 CANADA        3
                  BRASIL       6
                  BRASIL       7
                  MEXICO       8
Jane Borges
  • 552
  • 5
  • 14

1 Answers1

2

Use df.loc:

df.loc[df['Name'].isin(list_americ)]
OnY
  • 897
  • 6
  • 12