

Python Pandas : Access and change Column names & Row indexes in DataFrame
source link: https://www.tuicool.com/articles/hit/aEFv6jv
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In this article we will discuss how to get column names or Row Index names in DataFrame object.
First of all, create a dataframe object of students records i.e.
students = [ ('jack', 34, 'Sydeny') , ('Riti', 30, 'Delhi' ) , ('Aadi', 16, 'New York') ] # Create a DataFrame object dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'City'], index=['a', 'b', 'c'])
Contents of DataFrame objects are as follows,
Name Age City a jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York
Now let’s access column names and Row indexes,
Accessing & Modifying Column Names in DataFrame
Get all column names from DataFrame
DataFrame object has an Attribute columns that is basically an Index object and contains column Labels of Dataframe. We can get the ndarray of column names from this Index object i.e.
# Get ndArray of all column names columnsNamesArr = dfObj.columns.values
Its contents will be,
['Name' 'Age' 'City']
Get Column name by Index / position in DataFrame
As df.column.values is a ndarray, so we can access it contents by index too. So, let’s get the name of column at index 2 i.e.
dfObj.columns.values[2]
Changing the name of a Column in DataFrame
Any modification in this ndArray ( df.column.values ) will modify the actual DataFrame. For example let’s change the name of column at index 0 i.e.
# Get ndArray of all column names columnsNamesArr = dfObj.columns.values # Modify a Column Name columnsNamesArr[0] = 'Test'
This change will be reflected in linked DataFrame object too. Now contents of DataFrame object is,
Test Age City a jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York
But we convert it to list before modifying then changes will not be reflected in original DataFrame object.
For example create a list of Column Names of DataFrame i.e.
# get a copy list of all the column names columnsNames = list(dfObj.columns.values)
or
columnsNames = list(dfObj)
Now modify the list,
# Modify Column Name columnsNames[0] = 'Test_Name'
This modification will not be reflected in original DataFrame object and DataFrame object will remain same i.e.
Test Age City a jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York
Accessing & Modifying Row Indexes in DataFrame
Contents of dataframe objects () is as follows,
Test Age City a jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York
Let’s access and modify its row indexes,
Get List of all Row Indexes from DataFrame
To get the list of all row index names from a dataFrame object, use index attribute instead of columns i.e. df.index.values
# get a list of all the column names indexNamesArr = dfObj.index.values
It returns an ndarray of all row indexes in dataframe i.e.
['a', 'b', 'c']
Get Row Index name by position in DataFrame
As df.index.values is a ndarray, so we can access it contents by position too. So, let’s get the name of column at position 2 i.e.
dfObj.index.values[2]
Changing the name of a Row Index in DataFrame
Any modification in this ndArray ( df.index.values ) will modify the actual DataFrame. For example let’s change the name of row index at position 0 i.e.
# Modify a Row Index Name indexNamesArr[0] = 'P'
This change will be reflected in linked DataFrame object too. Now contents of DataFrame object is,
Test Age City P jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York
But if we convert it to list before modifying then changes will not be reflected in original DataFrame object. For example create a copy list of Row Index Names of DataFrame i.e.
# get a copy list of all the column names indexNames = list(dfObj.index.values)
Complete example is as follows :
import pandas as pd def main(): students = [ ('jack', 34, 'Sydeny') , ('Riti', 30, 'Delhi' ) , ('Aadi', 16, 'New York') ] # Create a DataFrame object dfObj = pd.DataFrame(students, columns = ['Name' , 'Age', 'City'], index=['a', 'b', 'c']) print("Original DataFrame : " , dfObj, sep="\n"); ''' Get All Columns Names in DataFrame ''' # Get ndArray of all column names columnsNamesArr = dfObj.columns.values print("Column Names : " , columnsNamesArr) ''' Get Columns Name by Index/position in DataFrame ''' print("Column Names at index 2: " , dfObj.columns.values[2]) ''' Pandas : Modify Column Name in DataFrame ''' # Modify a Column Name columnsNamesArr[0] = 'Test' print("Modified Column Names : " , columnsNamesArr) print("Modified DataFrame : ") print(dfObj) ''' Get Copy of all Columns Names in DataFrame ''' # get a copy list of all the column names columnsNames = list(dfObj.columns.values) print("Column Names : " , columnsNames) # Modify Column Name columnsNames[0] = 'Test_Name' print("Modified Column Names : " , columnsNames) print("DataFrame : ") print(dfObj) ''' Get List of All Index Names in DataFrame ''' # get a list of all the column names indexNamesArr = dfObj.index.values print("All Index Names : " , indexNamesArr) ''' Get Row Index Names in DataFrame by Poisition ''' print("Row Index Names at position 2: " , dfObj.index.values[2]) ''' Pandas : Modify Row Index Name in DataFrame ''' # Modify a Row Index Name indexNamesArr[0] = 'P' print("Modified Row Index Names : " , indexNamesArr) print("Modified DataFrame : ", dfObj, sep='\n') ''' Get Copy of all Row Index Names in DataFrame ''' # get a copy list of all the column names indexNames = list(dfObj.index.values) print("All Row Index Names : " , indexNames) if __name__ == '__main__': main()
Output:
Original DataFrame : Name Age City a jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York Column Names : ['Name' 'Age' 'City'] Column Names at index 2: City Modified Column Names : ['Test' 'Age' 'City'] Modified DataFrame : Test Age City a jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York Column Names : ['Test', 'Age', 'City'] Modified Column Names : ['Test_Name', 'Age', 'City'] DataFrame : Test Age City a jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York All Index Names : ['a' 'b' 'c'] Row Index Names at position 2: c Modified Row Index Names : ['P' 'b' 'c'] Modified DataFrame : Test Age City P jack 34 Sydeny b Riti 30 Delhi c Aadi 16 New York All Row Index Names : ['P', 'b', 'c']
Click Here to Subscribe for more Articles / Tutorials like this.
Recommend
-
122
Count number of Zeros in Pandas Dataframe Column This article will discuss how to count the number of zeros in a single or all column of a Pandas Dataframe. Let’s first create a Dataframe from a list of...
-
10
Pandas | Count non-zero values in Dataframe Column This article will discuss how to count the number of non-zero values in one or more Dataframe columns in Pandas. Let’s first create a Dataframe from a...
-
15
Pandas – Count True Values in a Dataframe Column In this article, we will discuss different ways to count True values in a Dataframe Column. First of all, we will create a Dataframe from a list of tuples...
-
16
This article will discuss how to rename column names in Pandas DataFrame. Table of Contents A DataFrame is a data structure that will store the data in rows and columns. We can create a DataFrame using pandas.D...
-
12
How to Drop Index Column of a Pandas DataFrame – thisPointer In this article, we will discuss about the different ways to drop the Index Column of a Pandas DataFrame. A DataFrame is a data structure that stores the data in rows...
-
14
Check if a Column exists in Pandas DataFrame In this article, we will discuss how to check if a column or multiple columns exist in a Pandas DataFrame or not. Suppose we have a DataFrame, ...
-
9
In this article, we will discuss different ways to delete a column from a DataFrame in Pandas. Table Of Contents What is Pandas DataFrame? Pandas DataFrame is a labelled two di...
-
12
Convert the Column type from String to Datetime format in Pandas DataframeConvert the Column type from String to Datetime format in Pandas Dataframe10 Views30/05/2022...
-
6
Add Column to Pandas DataFrame with constant value In this article, we will learn different ways to add a column in a DataFrame with a constant value. Table Of Contents Sup...
-
12
January 13, 2023 /
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK