Pandas DataFrame Keys Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, we will learn about the DataFrame.keys() method in the Python pandas library. The keys() method is used to retrieve the index or column names from a DataFrame. We will go through the syntax and examples of using this method.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL pandas(("`Pandas`")) -.-> pandas/DataSelectionGroup(["`Data Selection`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) pandas/DataSelectionGroup -.-> pandas/select_columns("`Select Columns`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills pandas/select_columns -.-> lab-68646{{"`Pandas DataFrame Keys Method`"}} python/lists -.-> lab-68646{{"`Pandas DataFrame Keys Method`"}} python/tuples -.-> lab-68646{{"`Pandas DataFrame Keys Method`"}} python/dictionaries -.-> lab-68646{{"`Pandas DataFrame Keys Method`"}} python/importing_modules -.-> lab-68646{{"`Pandas DataFrame Keys Method`"}} python/numerical_computing -.-> lab-68646{{"`Pandas DataFrame Keys Method`"}} python/data_analysis -.-> lab-68646{{"`Pandas DataFrame Keys Method`"}} python/build_in_functions -.-> lab-68646{{"`Pandas DataFrame Keys Method`"}} end

Importing the pandas library

First, we need to import the pandas library, which provides data structures and data analysis tools for working with structured data.

import pandas as pd

Creating a DataFrame

Next, let's create a DataFrame using the pd.DataFrame() function. We will pass a dictionary as the data parameter, with keys as the column names and values as the corresponding data.

df = pd.DataFrame({"A": [0, 1], "B": [3, 4], "C": [0, 1], "D": [3, 4]})
print("The DataFrame is:")
print(df)

Output:

The DataFrame is:
   A  B  C  D
0  0  3  0  3
1  1  4  1  4

Using the DataFrame.keys() method

Now, let's use the DataFrame.keys() method to retrieve the column names of the DataFrame. We can assign the result to a variable and print it to see the column names.

keys = df.keys()
print(keys)

Output:

Index(['A', 'B', 'C', 'D'], dtype='object')

Getting the info of the index

We can also use the DataFrame.keys() method to retrieve the index (row labels) of the DataFrame. Let's create another DataFrame with an index and use the method to get the index info.

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)
keys = df.keys()
print(keys)

Output:

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

Summary

In this lab, we learned about the DataFrame.keys() method in the pandas library. This method allows us to retrieve the index (row labels) or column names of a DataFrame. By using this method, we can easily access and work with the index or columns of the DataFrame.

Other Python Tutorials you may like