Pandas DataFrame keys() Method – Explained with Examples

The Pandas library in Python is a powerful tool for data manipulation and analysis. One of its many useful methods is the keys() method. This method is used to get the column labels of a DataFrame. Understanding how to use keys() can help streamline your data analysis process by providing a quick way to access the names of your DataFrame columns.

What is the keys() Method?

The keys() method in Pandas returns the column labels of the DataFrame. It is synonymous with the columns attribute. This can be especially useful when you want to iterate over column names or when you need to reference the column names programmatically.

Syntax

The syntax for the keys() method is straightforward:

Python
DataFrame.keys()
Parameters

The keys() method does not take any parameters.

Return Value

The method returns an Index object containing the column labels of the DataFrame.

Examples

Let’s dive into some examples to see how the keys() method works in practice.

Example 1: Basic Usage

Consider the following DataFrame:

Python
import pandas as pd

data = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'City': ['New York', 'Los Angeles', 'Chicago']
}

df = pd.DataFrame(data)
print(df)

Output:

Markdown
      Name  Age         City
0    Alice   25     New York
1      Bob   30  Los Angeles
2  Charlie   35      Chicago

To get the column labels of this DataFrame, use the keys() method:

Python
column_labels = df.keys()
print(column_labels)

Output:

Markdown
Index(['Name', 'Age', 'City'], dtype='object')

The output is an Index object containing the column labels: ['Name', 'Age', 'City'].

Example 2: Iterating Over Column Labels

You can use the keys() method to iterate over the column labels of a DataFrame. This can be useful when you need to perform operations on each column.

Python
for col in df.keys():
    print(f"Column Name: {col}")

Output:

Markdown
Column Name: Name
Column Name: Age
Column Name: City
Example 3: Using ‘keys()’ in a Function

The keys() method can be used within a function to dynamically access column labels. For instance, you can create a function that prints the column names and their corresponding data types:

Python
def print_column_info(dataframe):
    for col in dataframe.keys():
        print(f"Column: {col}, Data Type: {dataframe[col].dtype}")

print_column_info(df)

Output:

Markdown
Column: Name, Data Type: object
Column: Age, Data Type: int64
Column: City, Data Type: object

Comparison with ‘columns’ Attribute

The keys() method is equivalent to the columns attribute. Both can be used interchangeably to get the column labels of a DataFrame.

Python
print(df.keys())
print(df.columns)

Output:

Markdown
Index(['Name', 'Age', 'City'], dtype='object')
Index(['Name', 'Age', 'City'], dtype='object')

As seen, both keys() and columns provide the same output.

Practical differences between keys() and columns():
  • Method vs. Attribute: The primary difference is that keys() is a method while columns is an attribute. This means that keys() requires parentheses to be called, while columns does not.
  • Mutability: You can modify the column labels using the columns attribute but not with keys().
  • Readability and Convention: While both keys() and columns can be used interchangeably for accessing the column labels, columns is more commonly used in practice due to its simplicity and mutability.
Conclusion

The keys() method in Pandas is a simple yet powerful tool for accessing the column labels of a DataFrame. It can be used for a variety of purposes, such as iterating over columns, referencing column names dynamically, and more. By understanding how to use this method, you can enhance your data manipulation and analysis workflows in Pandas.

Happy coding!

Also Explore:

Leave a Comment