Pandas methods of modifying column names include using the rename() function to modify column names based on the dictionary, directly modifying the columns attribute and directly assigning values to modify column names, and using the set_axis() function to modify all column names at once. . Detailed introduction: 1. Use the rename() function, which accepts a dictionary as a parameter. The key of the dictionary is the original column name and the value is the new column name. 2. Directly modify the columns attribute. You can directly modify the columns attribute of the DataFrame object. Modify column names, etc.
The operating system for this tutorial: Windows 10 system, Python version 3.11.4, Dell G3 computer.
Several ways to modify column names in Pandas
1. Use the rename() function
Pandas provides rename () function, you can easily modify the column name. This function accepts a dictionary as a parameter, the keys of the dictionary are the original column names, and the values are the new column names. The following is a sample code that uses the rename() function to modify column names:
import pandas as pd # 创建一个DataFrame对象 data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]} df = pd.DataFrame(data) # 修改列名 df.rename(columns={'A': 'Column1', 'B': 'Column2', 'C': 'Column3'}, inplace=True) # 打印修改后的列名 print(df.columns)
The above code creates a DataFrame object, and then uses the rename() function to modify column names A, B, and C to Column1, Column2, and Column3. Finally, the modified column names are printed out, and the output result is: ['Column1', 'Column2', 'Column3'].
2. Directly modify the columns attribute
In addition to using the rename() function, you can also directly modify the columns attribute of the DataFrame object to modify the column name. The following is a sample code that directly modifies the columns attribute:
import pandas as pd # 创建一个DataFrame对象 data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]} df = pd.DataFrame(data) # 直接修改列名 df.columns = ['Column1', 'Column2', 'Column3'] # 打印修改后的列名 print(df.columns)
Compared with the first method, the above code only changes the rename() function to direct assignment. Finally, the modified column names are printed out, and the output result is: ['Column1', 'Column2', 'Column3'].
3. Use the set_axis() function
Pandas also provides the set_axis() function, which can modify all column names at once. This function accepts a list as a parameter, and the elements in the list are the new column names. The following is a sample code that uses the set_axis() function to modify column names:
import pandas as pd # 创建一个DataFrame对象 data = {'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]} df = pd.DataFrame(data) # 修改所有列名 df.set_axis(['Column1', 'Column2', 'Column3'], axis=1, inplace=True) # 打印修改后的列名 print(df.columns)
The above code creates a DataFrame object, and then uses the set_axis() function to modify all column names to Column1, Column2, and Column3. Finally, the modified column names are printed out, and the output result is: ['Column1', 'Column2', 'Column3'].
Summary:
This article introduces three common methods to modify column names in Pandas. Use the rename() function to modify column names based on the dictionary; directly modify the columns attribute to directly assign values to modify column names; use the set_axis() function to modify all column names at once. Choose the appropriate method to modify column names according to actual needs, which can facilitate data processing and analysis.
The above is the detailed content of How to modify column names in pandas. For more information, please follow other related articles on the PHP Chinese website!