Explanation of Pandas DataFrame to Modify Row and Column Names

Source: Internet
Author: User
Keywords dataframe pandas dataframe pandas dataframe tutorial
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')

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.