Pandas DataFrame keys() Method

In this tutorial, we will learn the Python pandas DataFrame.keys() method. Using this method we can get the ‘info axis’. This is the index for Series, columns for DataFrame.

The below shows the syntax of the DataFrame.keys() method.

Syntax

DataFrame.keys()

Example: The DataFrame.keys() Method

Let's create a DataFrame and get the information about the index of the DataFrame.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df=pd.DataFrame({"A":[0,1],"B":[3,4],"C":[0,1],"D":[3,4,]})
print("-----------The DataFrame is-------")
print(df)
print(df.keys())


-----------The DataFrame is-------
A B C D
0 0 3 0 3
1 1 4 1 4
Index(['A', 'B', 'C', 'D'], dtype='object')

Example: Getting the info of the index of the DataFrame using the DataFrame.keys() Method

The below example is similar to the previous one except, we are getting the information about the index of the DataFrame.

#importing pandas as pd
import pandas as pd
#creating DataFrame
df=pd.DataFrame({"Name":["Navya","Vindya"],"Age":[25,24],"Education":["M.Tech","Ph.d"]},index=['index_1', 'index_2'])
print("-----------The DataFrame is-------")
print(df)
print(df.keys())


-----------The DataFrame is-------
Name Age Education
index_1 Navya 25 M.Tech
index_2 Vindya 24 Ph.d
Index(['Name', 'Age', 'Education'], dtype='object')

Conclusion

In this tutorial, we learned the Python pandas DataFrame.keys() method. We learned the syntax and applying this method on the DataFrame to understand the DataFrame.keys() method.