When doing WISE data processing, sometimes it is necessary to generate a
DataFrame from several sets of data, but during the generation process, I generally do not set the column name (because this process may have many steps), so the final column name is default. In order to facilitate reading the code later, I still hope that the data that has been processed finally has corresponding data-related column names.
I also found some people's solutions on the Internet. I feel that none of them can understand. Now I will summarize my understanding as follows.
Method 1: Modify the column or index attribute value of the
DataFrame
DataFrame attribute link: DataFrame. Now we know that for each DataFrame there are two attributes, index and columns, which give information about the \color{red}{index} and \color{red}{column} of the Dataframe. Therefore, we can re-assign the Index or column attribute information of the DataFrame to achieve the renaming of rows or columns.
\color{red}{Note}: This method is to rename all the rows or columns at once. You cannot rename only a single row or a few rows or columns, because the value of the index or columns property of the DataFrame cannot be changed ( That is: the index attribute value can be assigned as a whole, but it cannot be assigned to a single or several).
import numpy as np
import pandas as pd
df = pd.DataFrame({
'col1':['a','a','b',np.nan,'c'],
'col2':[2, 1, 8, 7, 6],
'col3':[0, 4, 7, 2, 3],
})
df
col1 col2 col3
0 a 2 0
1 a 1 4
2 b 8 7
3 NaN 7 2
4 c 6 3
#Edit row label
df.columns
Index(['col1','col2','col3'], dtype='object')
df.columns = ['a','b','c']
df
a b c
0 a 2 0
1 a 1 4
2 b 8 7
3 NaN 7 2
4 c 6 3
#Edit column labels
df.index = ['a0','a1','a2','a3','a4']
df
col1 col2 col3
a0 a 2 0
a1 a 1 4
a2 b 8 7
a3 NaN 7 2
a4 c 6 3
Method 2: pandas.DataFrame.rename() function
The rename function is specifically to modify the DataFrame coordinate axis labeling function. The advantage of rename function: \color{red}{selectively modify} the label of a row and a column.
\color{red}{Note}: The value in the function/dictionary must be unique (1 to 1). Tags not included in the dictionary/Series will remain as they are. The extra tags listed will not cause an error.
DataFrame.rename(self, mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False, level=None, errors='ignore')