Home > OS >  How to change multiple values in the df under multiple conditions
How to change multiple values in the df under multiple conditions

Time:07-15

I know how to change values of the df under one condition (df_csv.loc[df_csv['X'] == 'train', ['A', 'B']] = ['t1', 't2'])`, but how do I change values in the df under multiple conditions?

What I've tried:

df_csv.loc[df_csv[['A', 'B']] == ['car', 'plane'], ['A', 'B']] = ['t1', 't2']

The error message I get is: KeyError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_8032/3727894698.py in 14 15 ---> 16 df_csv.loc[df_csv[['A', 'B']] == ['car', 'plane'], ['A', 'B']] = ['t1', 't2']

CodePudding user response:

try:

df_csv.loc[((df_csv['A'] == 'car') & (df_csv['B'] == 'plane')), ['A', 'B']] = ['t1', 't2']

to replicate everything:

# create data
import pandas as pd
data = {'A': ['train', 'car', 'truck', 'train', 'car', 'plane', 'plane', 'truck'],
        'B': ['car', 'train', 'truck', 'train', 'plane', 'plane', 'car', 'car']}
df_csv = pd.DataFrame(data)

print(df_csv) # before changing

# change based on multiple conditions
df_csv.loc[((df_csv['A'] == 'car') & (df_csv['B'] == 'plane')), ['A', 'B']] = ['t1', 't2']
print(df_csv) # after change

CodePudding user response:

You could use the following:

import pandas as pd

df = pd.DataFrame([["car","plane", 0], ["train","cat", 1], ["carriage", "car", 2]], columns=["A", "B", "C"])
df.loc[df[["A", "B"]].isin(["car", "plane"]).all(axis=1), ["A", "B"]] = ["t1", "t2"]

This gives you:

|    | A        | B   |   C |
|---:|:---------|:----|----:|
|  0 | t1       | t2  |   0 |
|  1 | train    | cat |   1 |
|  2 | carriage | car |   2 |
  • Related