Removing "\n" from columns name in Pandas dataFrame

Viewed 36

I have a CSV file having column names with line breaks when I read the file with pd.read_csv() it returns the column names like this Violent\ncrime\nrate. how do I replace \n with "_" for all these columns?

1 Answers

Try:

df.columns = [c.replace("\n", "_") for c in df.columns]
print(df)
Related