Home > Blockchain >  replacing figures in a column to others if condition is not met in panda dataframe
replacing figures in a column to others if condition is not met in panda dataframe

Time:10-25

I have a pandas dataframe with this column "Party". There are about 50 different names for "Party". I only want to retain 2 values 'democrat' and 'republican' and the rest all change to "Others". How do I go about doing it using isin function?

partytokeep = df.party_detailed.isin(['democrat', 'republican'])
partytokeep


df['party_detailed'].where(df.party_detailed == 'partytokeep'), other='others', inplace=True)
df

my syntax error is as such:
  File "<ipython-input-29-c7c062b05249>", line 1
    df['party_detailed'].where(df.party_detailed == 'partytokeep'), other='others', inplace=True)
                                                                                                ^
SyntaxError: invalid syntax

CodePudding user response:

There is a missing ( in line df['party_detailed'].where(df.party_detailed == 'partytokeep'), other='others', inplace=True)

The error SyntaxError: invalid syntax tells the same, that it has to do somthing with syntax. The correct syntax would be:

df['party_detailed'].where((df.party_detailed == 'partytokeep'), other='others', inplace=True)

Also once you have calculated boolian partytokeep you can directy set values

df.loc[~partytokeep, 'party_detailed'] = 'others'

CodePudding user response:

i did the above and all my values all changed to 'Others' instead of keeping 'democrat' and 'republican'and replacing all others to 'Others'

  • Related