ladybug
ladybug

Reputation: 109

Fill panda columns with conditions

I'm trying to fill a column C with conditions: if the value of column B is None, then fill column C with the value of column A. If column B is not None, then fill column C with the value 3

I tried:

import pandas
df = pandas.DataFrame([{'A': 5, 'B': None, 'C': ''},
                   {'A': 2, 'B': "foo", 'C': ''},
                   {'A': 6, 'B': "foo", 'C': ''},
                   {'A': 1, 'B': None, 'C': ''}])

df["C"] = df["B"].apply(lambda x: 3 if (x != None) else df["A"])

My output:

TypeError: object of type 'int' has no len()

I know the problem is df["A"], but I don't know how to solve it

Good output:

df = pandas.DataFrame([{'A': 5, 'B': None, 'C': 5},
                   {'A': 2, 'B': "foo", 'C': 3},
                   {'A': 6, 'B': "foo", 'C': 3},
                   {'A': 1, 'B': None, 'C': 1}])

Upvotes: 0

Views: 40

Answers (1)

jezrael
jezrael

Reputation: 862651

Use numpy.where with test None by Series.isna:

df["C"] = np.where(df["B"].isna(), df['A'], 3)
#alternative
#df["C"] = df['A'].where(df["B"].isna(), 3)
   print (df)
 A     B  C
0  5  None  5
1  2   foo  3
2  6   foo  3
3  1  None  1

Upvotes: 2

Related Questions