mercredi 24 juillet 2019

Is it possible to change 2 columns at same time with if-condition in a DataFrame?

I was working with some DataFrames on python and I had a situation where I had to change 2 columns at same time in a same if condition. I solved my problem but I didn't do it with only one if-condition.

I tried to search something about that but I only found when the if condition has 2 or more columns, and not when the condition is satisfied.

Suppose we have:

data.head()

    Foo
1     a
2     a
3     b
4     b
5     b

If data['Foo'] == a, we do data['Foo'] = c et data['Bar'] = 10, else data['Bar'] = 0. So

data.head()

    Foo  Bar 
1     c   10 
2     c   10
3     b    0
4     b    0
5     b    0

I solved that using twice np.where(). Do I have to use apply()?

Aucun commentaire:

Enregistrer un commentaire