I want to change the existing dataframe value to nan. What is the way to do it when you need to change several?
dataframe['A', 'B'....] = np.nan
I tried this but nothing changed
I want to change the existing dataframe value to nan. What is the way to do it when you need to change several?
dataframe['A', 'B'....] = np.nan
I tried this but nothing changed
Double brackets are required in this case, to pass the list of columns to the dataframe index operator []. For the OP case would be:
dataframe[['A', 'B'....]] = np.nan
Reproducible example:
import numpy as np
import pandas as pd
dict= {'ColA':[1,2,3], 'ColB':[4,5,6], 'ColC':[7,8,9], 'ColD':[-1,-2,-3] }
df=pd.DataFrame(dict,index=['I1','I2','I3'])
print(df)
df[['ColA','ColD']]=np.nan
print(df)
Note: This solution was originally suggested via comment, now included as an answer with a reproducible example for future reference.