vendredi 22 juin 2018

Looping in Python: modify one column based on values in other columns

Current data:

Stored as a Pandas DataFrame

print(df)

col1 | col2 
A    | 1
B    | 3
C    | 3
D    | 7
E    | 4
C    | 3

Goal:

I want to create a new column that adds 1 to col2 if col1 is either A, C, or E.

col1 | col2  | col2_corrected
A    | 1     | 2
B    | 3     | 3
C    | 3     | 4
D    | 7     | 7
E    | 4     | 5
C    | 3     | 4

My failed solution:

add_one_to_me = ['A','C','E']

if df.col1.isin(add_one_to_me):
    df.col2_corrected = df.col2 + 1
else: df.col2_corrected = df.col2

This throws an error about ambiguous truth because it is assessing the truth of the entire series.

How do I apply this to each row of the DataFrame? I am new to Python and programming, so it's a pretty basic question.

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire