Home > Net >  Pandas replace with dict and condition
Pandas replace with dict and condition

Time:12-07

In Pandas in Python you have the function df.replace(), which you can give a dict to change the values in a column:

df = pd.DataFrame({'A': [0, 1, 2, 3, 4],
                   'B': [5, 6, 7, 8, 9],
                   'C': ['a', 'b', 'c', 'd', 'e']})
df.replace('A': {0: 10, 3: 100})

Is it possible to add a condition to this? For example that it will only replace the values in the A column if the value in the B column is smaller than 8.

CodePudding user response:

Try this:

df.update(df['A'][df['B'] < 8].replace({0: 10, 3: 100}))

Output:

>>> df
      A  B  C
0  10.0  5  a
1   1.0  6  b
2   2.0  7  c
3   3.0  8  d
4   4.0  9  e

Notice how A at row 3 is not 100, but 3.0 (the old value). Because B at row 3 is 8, which, per your condition, is not less then 8.

CodePudding user response:

You can use boolean indexing and loc:

df.loc[df['B']<8] = df.loc[df['B']<8].replace({'A': {0: 10, 3: 100}})

Output:

    A  B  C
0  10  5  a
1   1  6  b
2   2  7  c
3   3  8  d
4   4  9  e

CodePudding user response:

Using where:

df['A'] = df['A'].replace({0: 10, 3: 100}).where(df['B'].lt(8), df['A'])

output:

    A  B  C
0  10  5  a
1   1  6  b
2   2  7  c
3   3  8  d
4   4  9  e
  • Related