Methods to get rows and columns names in dataframe
In this we will study different methods to get rows and column names in a dataframe.
Methods to get column name in dataframe
-
Method 1: By iterating over columns
In this method, we will simply be iterating over all the columns and print the names of each column. Point to remember that dataframe_name. columns give a list of columns.Let see this with the help of an example.
import pandas as pd import numpy as np students = [('Raj', 24, 'Mumbai', 95) , ('Rahul', 21, 'Delhi' , 97) , ('Aadi', 22, 'Kolkata', 81) , ('Abhay', 24,'Rajasthan' ,76) , ('Ajjet', 21, 'Delhi' , 74)] # Create a DataFrame object df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) print("Original Dataframe\n") print(df,'\n') print(df.columns,'\n') print("columns are:") for column in df.columns: print(column,end=" ")
Output
Original Dataframe
Name Age City Marks
0 Raj 24 Mumbai 95
1 Rahul 21 Delhi 97
2 Aadi 22 Kolkata 81
3 Abhay 24 Rajasthan 76
4 Ajjet 21 Delhi 74
Index(['Name', 'Age', 'City', 'Marks'], dtype='object')
columns are:
Name Age City Marks
Here we see that df. columns give a list of columns and by iterating over this list we can easily get column names.
-
Method 2-Using columns.values
columns. values return an array of column names. Let see this with the help of an example.
import pandas as pd import numpy as np students = [('Raj', 24, 'Mumbai', 95) , ('Rahul', 21, 'Delhi' , 97) , ('Aadi', 22, 'Kolkata', 81) , ('Abhay', 24,'Rajasthan' ,76) , ('Ajjet', 21, 'Delhi' , 74)] # Create a DataFrame object df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) print("Original Dataframe\n") print(df,'\n') print("columns are:") print(df.columns.values,'\n')
Output
Original Dataframe Name Age City Marks 0 Raj 24 Mumbai 95 1 Rahul 21 Delhi 97 2 Aadi 22 Kolkata 81 3 Abhay 24 Rajasthan 76 4 Ajjet 21 Delhi 74 columns are: ['Name' 'Age' 'City' 'Marks']
-
Method 3- using tolist() method
Using tolist() method with values with given the list of columns. Let see this with the help of an example.
import pandas as pd import numpy as np students = [('Raj', 24, 'Mumbai', 95) , ('Rahul', 21, 'Delhi' , 97) , ('Aadi', 22, 'Kolkata', 81) , ('Abhay', 24,'Rajasthan' ,76) , ('Ajjet', 21, 'Delhi' , 74)] # Create a DataFrame object df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) print("Original Dataframe\n") print(df,'\n') print("columns are:") print(df.columns.values.tolist(),'\n')
Output
Original Dataframe Name Age City Marks 0 Raj 24 Mumbai 95 1 Rahul 21 Delhi 97 2 Aadi 22 Kolkata 81 3 Abhay 24 Rajasthan 76 4 Ajjet 21 Delhi 74 columns are: ['Name', 'Age', 'City', 'Marks']
-
Method 4- Access specific column name using index
As we know that columns. values give an array of columns and we can access array elements using an index. So in this method, we use this concept. Let see this with the help of an example.
import pandas as pd import numpy as np students = [('Raj', 24, 'Mumbai', 95) , ('Rahul', 21, 'Delhi' , 97) , ('Aadi', 22, 'Kolkata', 81) , ('Abhay', 24,'Rajasthan' ,76) , ('Ajjet', 21, 'Delhi' , 74)] # Create a DataFrame object df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) print("Original Dataframe\n") print(df,'\n') print("columns at second index:") print(df.columns.values[2],'\n')
Output
Original Dataframe Name Age City Marks 0 Raj 24 Mumbai 95 1 Rahul 21 Delhi 97 2 Aadi 22 Kolkata 81 3 Abhay 24 Rajasthan 76 4 Ajjet 21 Delhi 74 columns at second index: City
So these are the methods to get column names.
Method to get rows name in dataframe
-
Method 1-Using index.values
As columns., values give a list or array of columns similarly index. values give a list of array of indexes. Let see this with the help of an example.
import pandas as pd import numpy as np students = [('Raj', 24, 'Mumbai', 95) , ('Rahul', 21, 'Delhi' , 97) , ('Aadi', 22, 'Kolkata', 81) , ('Abhay', 24,'Rajasthan' ,76) , ('Ajjet', 21, 'Delhi' , 74)] # Create a DataFrame object df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) print("Original Dataframe\n") print(df,'\n') print("Rows are:") print(df.index.values,'\n')
Output
Original Dataframe Name Age City Marks 0 Raj 24 Mumbai 95 1 Rahul 21 Delhi 97 2 Aadi 22 Kolkata 81 3 Abhay 24 Rajasthan 76 4 Ajjet 21 Delhi 74 Rows are: [0 1 2 3 4]
-
Method 2- Get Row name at a specific index
As we know that index. values give an array of indexes and we can access array elements using an index. So in this method, we use this concept. Let see this with the help of an example.
import pandas as pd import numpy as np students = [('Raj', 24, 'Mumbai', 95) , ('Rahul', 21, 'Delhi' , 97) , ('Aadi', 22, 'Kolkata', 81) , ('Abhay', 24,'Rajasthan' ,76) , ('Ajjet', 21, 'Delhi' , 74)] # Create a DataFrame object df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) print("Original Dataframe\n") print(df,'\n') print("Row at index 2:") print(df.index.values[2],'\n')
Output
Original Dataframe Name Age City Marks 0 Raj 24 Mumbai 95 1 Rahul 21 Delhi 97 2 Aadi 22 Kolkata 81 3 Abhay 24 Rajasthan 76 4 Ajjet 21 Delhi 74 Row at index 2: 2
-
Method 3-By iterating over indices
As dataframe_names.columns give a list of columns similarly dataframe_name.index gives the list of indexes. Hence we can simply be iterating over all lists of indexes and print rows names. Let see this with help of an example.
import pandas as pd import numpy as np students = [('Raj', 24, 'Mumbai', 95) , ('Rahul', 21, 'Delhi' , 97) , ('Aadi', 22, 'Kolkata', 81) , ('Abhay', 24,'Rajasthan' ,76) , ('Ajjet', 21, 'Delhi' , 74)] # Create a DataFrame object df = pd.DataFrame( students, columns=['Name', 'Age', 'City', 'Marks']) print("Original Dataframe\n") print(df,'\n') print("List of indexes:") print(df.index,'\n') print("Indexes or rows names are:") for row in df.index: print(row,end=" ")
Output
Original Dataframe Name Age City Marks 0 Raj 24 Mumbai 95 1 Rahul 21 Delhi 97 2 Aadi 22 Kolkata 81 3 Abhay 24 Rajasthan 76 4 Ajjet 21 Delhi 74 List of indexes: RangeIndex(start=0, stop=5, step=1) Indexes or rows names are: 0 1 2 3 4
So these are the methods to get rows and column names in the dataframe using python.
Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.
Read more Articles on Python Data Analysis Using Padas – Select items from a Dataframe
- Select Rows & Columns in a Dataframe using loc & iloc in
- Select Rows in a Dataframe based on conditions
- Get minimum values in rows or columns & their index position in Dataframe
- Get unique values in columns of a Dataframe
- Select first or last N rows in a Dataframe using head() & tail()
- Get DataFrame contents as a list of rows or columns (list of lists)