简体   繁体   中英

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'])

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(' ','_'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM