Pandas set multiple column and row values to nan based on another dataframe


Zanam

I have two large dataframes df1:

Col1    Col2    Val1    Val2    Val3
asd     ASYL    4.2             4.2
ppq     CONE            35      35
DA      HU      100     100

and df2is

Col1    Col2    Val1    Val2  Val3
asd     ASYL    7       12    17
ppq     CONE    17      19    19
DA      HU      5       14    13

The two dataframes have the same index column Col1and Col2the same value column Val1, Val2, Val3.

I'm trying to set the value df2to where nanit is Nullgetting df1the following:

Col1    Col2    Val1    Val2  Val3
asd     ASYL    7             17
ppq     CONE            19    19
DA      HU      5       14      

I have tried the following:

idx = df1.isnull()
df2.loc[idx] = np.nan

But it doesn't work.

Ch3steR:

you can use withdf.wheredf.notna

df2.where(df1.notna())

Col1    Col2    Val1   Val2  Val3
asd     ASYL    7      NaN    17
ppq     CONE   NaN      19    19
DA      HU      5       14    NaN   

Related


Set nan to row in pandas dataframe based on column value

Leonardo Ferreira use: import numpy as np import pandas as pd import matplotlib.pyplot as plt a = pd.read_csv('file.csv', na_values=['-9999.0'], decimal=',') a.index = pd.to_datetime(a[['Year', 'Month', 'Day', 'Hour', 'Minute']]) pd.options.mode.chained_assig

Set nan to row in pandas dataframe based on column value

Leonardo Ferreira use: import numpy as np import pandas as pd import matplotlib.pyplot as plt a = pd.read_csv('file.csv', na_values=['-9999.0'], decimal=',') a.index = pd.to_datetime(a[['Year', 'Month', 'Day', 'Hour', 'Minute']]) pd.options.mode.chained_assig

Set nan to row in pandas dataframe based on column value

Leonardo Ferreira use: import numpy as np import pandas as pd import matplotlib.pyplot as plt a = pd.read_csv('file.csv', na_values=['-9999.0'], decimal=',') a.index = pd.to_datetime(a[['Year', 'Month', 'Day', 'Hour', 'Minute']]) pd.options.mode.chained_assig

Set multiple column values to NaN based on condition

Nebula Man I'm trying to set values in 2 columns of a dataframe to null based on a condition applied to one of the columns. I know how to set the value of 1 column to null based on a condition. I do this using col3 in the example below. My question is how can

Set multiple column values to NaN based on condition

Nebula Man I'm trying to set values in 2 columns of a dataframe to null based on a condition applied to one of the columns. I know how to set the value of 1 column to null based on a condition. I do this using col3 in the example below. My question is how can

Set multiple column values to NaN based on condition

Nebula Man I'm trying to set values in 2 columns of a dataframe to null based on a condition applied to one of the columns. I know how to set the value of 1 column to null based on a condition. I do this using col3 in the example below. My question is how can

Set multiple column values to NaN based on condition

Nebula Man I'm trying to set values in 2 columns of a dataframe to null based on a condition applied to one of the columns. I know how to set the value of 1 column to null based on a condition. I do this using col3 in the example below. My question is how can