Manipulate/Recode Pandas dataframe

Viewed 43

Working on some simple data manipulation in Pandas and uncertain how to do something along the lines of the logic defined below. I'm trying to change values in Column C based on values in Column A in this sample idea. Suggestions?

df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
                   'B': [5, 6, 7, 8, 9],
                   'C': ['a', 'b', 'c', 'd', 'e']})
if df['A'] < 2:
    df['C'] = "Small"
else:
    df['C'] = "Big"
1 Answers

Conditions work different in pandas. You can try

import numpy as np
df['C'] = np.where(df.A < 2, 'small','big')
df

Output

   A  B      C
0  0  5  small
1  1  6  small
2  2  7    big
3  3  8    big
4  4  9    big
Related