Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas
communitycreator

What is the DataFrame.keys() method in pandas?

AKASH BAJWA

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

In pandas, the DataFrame.keys() method is used to get the axis info for pandas instance. Pandas instance or object can be either Series or a DataFrame.

  • DataFrame: In the case of DataFrame, it returns an index that contains column names and dataType.
  • Series: For Series, it returns RangeIndex that contains start, stop and step values.

    Note: Axis info contains column names and data type.

    Syntax


    DataFrame.keys()

    Parameter

    It does not take any argument value.

    Return value

    This method returns a Python iterator object.

    DataFrames

    In this example, we discuss keys() with DataFrame:

    # importing pandas as pd alias
    import pandas as pd
    # initializing a nested list
    data = [['China', 15], ['America', 21], ['Pakistan', 0.4]]
    # Creating the DataFrame
    df = pd.DataFrame(data, columns = ['Country', 'GDP'])
    # Print the DataFrame
    print(df.keys())
    Using the keys() function on a DataFrame

    In the code snippet above, we create a DataFrame with column names Country and GDP. Next, we invoke the keys() function to get the info axis.

    • Line 4: We create a nested list instance that includes countries and their 2021 GDPs.
    • Line 6: We use pandas to create a DataFrame by using Country and GDP as column names.
    • Line 8: Now, calling keys() to extract info axis.

    Series

    In this example, we discuss keys() with Series:

    # importing pandas as pd
    import pandas as pd
    import numpy as np
    # creating a numpy array
    data = np.array(['e','d','u','c','a','t','i','v','e'])
    # creating a series
    _series = pd.Series(data)
    # to find the index
    print(_series.keys())
    Using the keys() on a Series

    In the code snippet above, we create a series of the character array and invoking keys() to get axis info.

    • Line 5: We create a NumPy array.
    • Line 7: We invoke Series() to convert above create character array into a Series object.
    • Line 9: Next, we call keys() to extract info axis.

    RELATED TAGS

    pandas
    communitycreator

    Grokking Modern System Design Interview for Engineers & Managers

    Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

    Keep Exploring