[Python Fennel Bean Series] PANDAS Modify DataFrame column name

[Python Fennel Bean Series] PANDAS Modify DataFrame column name

Programming in Python, using different methods to accomplish the same goal, is sometimes a very interesting thing. This reminds me of Kong Yiji in Lu Xun's works. Kong Yiji has done a lot of research on the four writing styles of fennel beans. I dare not compare myself to Kong Yiji, here are some Python fennel beans, for all coders.

First prepare a function to generate a DataFrame for testing. This DataFrame has 3 columns named a, b and c.

import pandas as pd
def get_df():
    return pd.DataFrame({
    
    'a':[1,2,3], 'b':[4,5,6], 'c':[7,8,9]})
get_df()
a b c
0 1 4 7
1 2 5 8
2 3 6 9

Fennel Bean One: The columns property

One day, BOSS said that the column names of the DataFrame should be changed to uppercase. So I simply threw a List to the columns property of the DataFrame, and the task was completed. Examples are as follows:

df = get_df()
df.columns=['A','B','C']
df
A B C
0 1 4 7
1 2 5 8
2 3 6 9

Fennel Bean II: columns.str

One day, BOSS gave me a DataFrame with 300 columns and said that the column names should be changed to uppercase. Obviously, using the above method is a waste of life. So I used columns.str. Examples are as follows:

df = get_df()
df.columns = df.columns.str.upper()
df
A B C
0 1 4 7
1 2 5 8
2 3 6 9

Fennel beans three: rename method 1

One day, BOSS gave me a DataFrame with 300 columns and said that column a should be renamed to A, column c should be renamed to C, and other columns should remain unchanged. So I used the rename method. Examples are as follows:

df = get_df()
df.rename(columns={
    
    'a': 'A', 'c': 'C'})
A b C
0 1 4 7
1 2 5 8
2 3 6 9

The rename method has an inplace parameter, and the default value is False. In the above example, df has not changed.

df
a b c
0 1 4 7
1 2 5 8
2 3 6 9

If you want to change it, you need to set inplace to True.

df.rename(columns={
    
    'a': 'A', 'c': 'C'}, inplace=True)
df
A b C
0 1 4 7
1 2 5 8
2 3 6 9

Fennel beans four: rename method 2

Of course, functions can also be used, for example:

df.rename(columns=lambda x:x.upper())
A B C
0 1 4 7
1 2 5 8
2 3 6 9

Fennel beans five: rename method 3

The columns parameter is not used here, for example:

df.rename(str.upper, axis=1)
A B C
0 1 4 7
1 2 5 8
2 3 6 9

rename is a good thing. For detailed usage, please see: official documentation .
So what are the benefits of using rename? Let’s give an example:

df = get_df()
df.T.rename(columns=lambda x:x+1).T
a b c
1 1 4 7
2 2 5 8
3 3 6 9

Fennel beans six: delete and add

If I'm feeling particularly happy, I might consider:

df = get_df()
for k, v in {
    
    'a': 'A','b': 'B', 'c': 'C'}.items():
    df[v] = df.pop(k)
df
A B C
0 1 4 7
1 2 5 8
2 3 6 9

Guess you like

Origin blog.csdn.net/mouse2018/article/details/113407386