I have a dataframe (in Python) as follows:
A B C D E F G H
0 T-1 2 3 - M-7 2 2
1 T-2 5 4 2 M-7 4 3
2 T-3 - - 3 M-7 9 4
3 T-4 6 - 4 M-7 - 9 5
4 T-5 - 1 5 M-7 8 6
I would like to replace the dashes (excluding those in column A and E) with NaN
.
I tried df.replace('-', np.nan)
.It ended up with replacing the entire cells of columns A and E as well. I guess I can use df[column_name].replace('-',np.nan)
but then I would have to apply it for each column separately.
Is there any other way, which is efficient and will work for any number of columns with similar restrictions?
df.replace('-', np.nan)
should work. Not sure why it's not working for you. – Dioscuridf.replace('-', np.nan)
works more than I require. I would like columns A and E remain as they are but the dashes of other columns should be replaced. – Chiccorydf.replace('-', np.nan)
shouldn't change anything in column A and E. It should only replace the dashes in the other columns. It should do exactly what you want, so I don't understand why it's not working for you. I can't reproduce this problem. – Dioscuri