pandas replace column name spaces with underscore

import pandas as pd 

# remove spaces in columns name
df.columns = df.columns.str.replace(' ','_')

Here is what the above code is Doing:
1. We’re using the .columns attribute of the df object to access the column names.
2. We’re using the .str attribute of the column names to access the string methods.
3. We’re using the .replace() method to replace all spaces with underscores.
4. We’re assigning the result back to the df.columns attribute.