I have a data frame df which has columns A,B,C,D,...,X,Y,Z and I wish to replace all 0 entries with 1 in all but columns C and D.
The following code that I am have attempted is:
df[df.columns[~df.columns.isin(['C','D'])]].replace(0,1,inplace=True)
but it is not working.
New to coding so any help here would be greatly appreciated!
I have a data frame df which has columns A,B,C,D,...,X,Y,Z and I wish to replace all 0 entries with 1 in all but columns C and D.
# Save the columns
c = df['C']
d = df['D']
# Remove the columns
df.drop('C', axis=1, inplace=True)
df.drop('D', axis1=, inplace=True)
# Make your replacement
df.replace(0, 1)
# Replace the columns
df['C'] = c
df['D'] = d
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With