Home > database >  pd.where() gives TypeError: where() got multiple values for argument 'other' when there is
pd.where() gives TypeError: where() got multiple values for argument 'other' when there is

Time:02-25

I've read the docs,

(https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.where.html)

When I use the below, all is fine and the code works perfectly:

df['c06new']='not_women_gathered'
df[c06new].where(f1 & f2,"women_gathered",inplace=True)
(df[c06new]=='women_gathered').sum()

However, if I use,

df.where(f1 & f2,"women_gathered", other='not',inplace=True)

I get: TypeError: where() got multiple values for argument 'other'

Why is this?

CodePudding user response:

Your code is producing an error because you're supplying other as a positional argument, but also as a keyword. For example, this gives us your error, TypeError: where() got multiple values for argument 'other.'

import pandas as pd
import numpy as np

df = pd.DataFrame({'A':range(10)})
df.where(df > 5, 10, other=12)

However, these two examples give the expected result.

df.where(df > 5, other=12)
df.where(df > 5, 12)

Output:

A
0   12
1   12
2   12
3   12
4   12
5   12
6   6
7   7
8   8
9   9

As suggested by Emma, you might be looking for np.where().

df['A'] = np.where(df > 5, 99, 12)

Output on original df:

A
0   12
1   12
2   12
3   12
4   12
5   12
6   99
7   99
8   99
9   99
  • Related