Home >Backend Development >Python Tutorial >Learn how to simply change column names of a Pandas dataframe

Learn how to simply change column names of a Pandas dataframe

王林
王林Original
2024-01-09 15:46:05607browse

Learn how to simply change column names of a Pandas dataframe

Pandas Tutorial: Easily learn how to modify column names, specific code examples are required

Introduction:
Pandas is a widely used data analysis library in Python, providing A large number of powerful functions, including data import, processing, conversion and analysis. During data processing, column names often need to be modified. This article will introduce in detail how to use the methods in the Pandas library to easily modify the column names of the data frame, and provide specific code examples.

  1. Import Pandas library
    First, we need to import the Pandas library in the Python script.
import pandas as pd
  1. Create a data frame
    To demonstrate how to modify column names, we first need to create a data frame.
data = {'Name': ['Tom', 'John', 'Alice', 'Emma'],
        'Age': [25, 30, 28, 35],
        'Gender': ['Male', 'Male', 'Female', 'Female']}
df = pd.DataFrame(data)
print(df)

The output results are as follows:

   Name  Age  Gender
0   Tom   25    Male
1  John   30    Male
2 Alice   28  Female
3  Emma   35  Female
  1. Use the rename method to modify the column names
    Pandas provides the rename method to modify the column names of the data frame. We can pass in a dictionary through this method, using the original column name as the key and the new column name as the value to achieve modification. The following is a specific code example:
new_columns = {'Name': '姓名', 'Age': '年龄', 'Gender': '性别'}
df = df.rename(columns=new_columns)
print(df)

The output results are as follows:

     姓名  年龄      性别
0   Tom  25    Male
1  John  30    Male
2 Alice  28  Female
3  Emma  35  Female
  1. Use the set_axis method to modify the column name
    In addition to using the rename method, we can also use set_axis Method to modify column names. The set_axis method requires passing in a list, where each element corresponds to a new column name. The following is a specific code example:
new_columns = ['姓名', '年龄', '性别']
df.set_axis(new_columns, axis='columns', inplace=True)
print(df)

The output result is the same as above:

     姓名  年龄      性别
0   Tom  25    Male
1  John  30    Male
2 Alice  28  Female
3  Emma  35  Female
  1. Use the columns attribute to directly modify the column name
    In addition, we can also directly Modify the column names by modifying the columns property of the data frame. The following is a specific code example:
df.columns = ['姓名', '年龄', '性别']
print(df)

The output result is the same as before:

     姓名  年龄      性别
0   Tom  25    Male
1  John  30    Male
2 Alice  28  Female
3  Emma  35  Female

Summary:
In this article, we introduced how to use the rename method in the Pandas library , set_axis method and directly modify the columns attribute to modify the column names of the data frame. These methods provide a flexible and concise way to modify column names to facilitate data processing and analysis. I hope this article will help you learn and use the Pandas library.

The above is the detailed content of Learn how to simply change column names of a Pandas dataframe. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn