Skip to content Skip to sidebar Skip to footer

Pandas Overwrite Values In Column Selectively Based On Condition From Another Column

I have a dataframe in pandas with four columns. The data consists of strings. Sample: A B C D 0 2 asicdsada

Solution 1:

As you've already tried, use str.contains to get a boolean Series, and then use .loc to say "change these rows and the D column". For example:

In [5]: df.loc[df["C"].str.contains("V"), "D"] = "a"

In [6]: df
Out[6]: 
    A               B             C  D
0   2       asicdsada       v:cVccv  a
1   4  ascccaiiidncll  v:cVccv:ccvc  a
2   9             sca           V:c  a
3  11            lkss          v:cv  u
4  13           lcoao         v:ccv  u
5  14        wuduakkk      V:ccvcv:  a

(Avoid using .ix -- it's officially deprecated now.)

Post a Comment for "Pandas Overwrite Values In Column Selectively Based On Condition From Another Column"