PostHeaderIcon Rename column headers in Pandas

When importing a file into a Pandas DataFrame, Pandas will use the first line of the file as the column names. If you have repeated names, Pandas will add .1 to the column name, sometime it’s bad about it. The following recipe shows you how to rename the column headers in a Pandas DataFrame.

import pandas as pd
import numpy as np
data_file = '~/Downloads/data/company.csv'
companys = pd.DataFrame.from_csv(data_file,
                        header=0,
                        sep=',',
                        index_col=0,
                        encoding=None,
                        tupleize_cols=False)

print(companys.columns)
companys.rename(columns={
                        '公司名': 'company',
                        '联系人': 'contact',
                        '职位': 'title',
                        '法人': 'legal_person',
                        '手机': 'mobile',
                        '座机': 'tel',
                        '传真': 'fax'}, inplace=True)
print(companys.columns)

let’s see the result of it.
before replace:

after replace:

tips of it:
With a Pandas DataFrame full of company data, we give Pandas a dictionary containing the column headers that we want to change and what we want to change them to. In the preceding recipe, we give the inplace=True argument to update the DataFrame. If we did not set inplace=True, the DataFrame wouldn’t be updated.

1648 views

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Copyright © 2010 - C++ Technology. All Rights Reserved.

Powered by Jerry | Free Space Provided by connove.com