Aug-14-2019, 11:34 PM
(This post was last modified: Aug-14-2019, 11:34 PM by boring_accountant.)
Assuming you want to replace all columns containing the string 'Unnamed' with the string 'nan', this will do:
print(df) df.columns = [col if not 'Unnamed' in col else 'nan' for col in df.columns] print(df)
Output: First Unnamed: 1 Unnamed: 2
0 A 1 3
1 B 2 4
First nan nan
0 A 1 3
1 B 2 4
As an alternative, the following yields equal results. I'm not sure whichever I prefer but both are possible.df.columns = df.columns.map(lambda col: col if not 'Unnamed' in col else 'nan')