NonProphetApostle
NonProphetApostle

Reputation: 31

Cleaning column names in pandas

I have a Dataframe I receive from a crawler that I am importing into a database for long-term storage.

The problem I am running into is a large amount of the various dataframes have uppercase and whitespace.

I have a fix for it but I was wondering if it can be done any cleaner than this:

def clean_columns(dataframe):
for column in dataframe:
    dataframe.rename(columns = {column : column.lower().replace(" ", "_")},
                    inplace = 1)
return dataframe

print(dataframe.columns)

Index(['Daily Foo', 'Weekly Bar'])

dataframe = clean_columns(dataframe)
print(dataframe.columns)

Index(['daily_foo', 'weekly_bar'])

Upvotes: 2

Views: 3863

Answers (1)

Anurag Dabas
Anurag Dabas

Reputation: 24322

You can try via columns attribute:

df.columns=df.columns.str.lower().str.replace(' ','_')

OR

via rename() method:

df=df.rename(columns=lambda x:x.lower().replace(' ','_'))

Upvotes: 9

Related Questions