CLASS 12 DATAFRAME ATTRIBUTES

 

Introduction

A DataFrame in pandas is a two-dimensional labeled data structure with columns of potentially different types. It's similar to a table in a database or a data frame in R. Understanding DataFrame attributes is crucial for effective data manipulation and analysis.

Common DataFrame Attributes

  1. .shape

    • Returns a tuple representing the dimensionality of the DataFrame.
    • Example:

      df.shape
  2. .size

    • Returns the number of elements in the DataFrame.
    • Example:

      df.size
  3. .ndim

    • Returns the number of dimensions of the DataFrame.
    • Example:

      df.ndim
  4. .dtypes

    • Returns the data types of each column.
    • Example:
      df.dtypes
  5. .columns

    • Returns the column labels of the DataFrame.
    • Example:

      df.columns
  6. .index

    • Returns the row labels of the DataFrame.
    • Example:
      df.index
  7. .values

    • Returns the underlying data of the DataFrame as a NumPy array.
    • Example:

      df.values

Examples

Let's consider the following DataFrame for examples:


import pandas as pd data = { 'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [24, 27, 22], 'Salary': [70000, 80000, 65000] } df = pd.DataFrame(data)
  1. .shape


    print(df.shape) # Output: (3, 3)
  2. .size


    print(df.size) # Output: 9
  3. .ndim

    print(df.ndim) # Output: 2
  4. .dtypes

    print(df.dtypes) # Output: # Name object # Age int64 # Salary int64 # dtype: object
  5. .columns


    print(df.columns) # Output: Index(['Name', 'Age', 'Salary'], dtype='object')
  6. .index


    print(df.index) # Output: RangeIndex(start=0, stop=3, step=1)
  7. .values


    print(df.values) # Output: # [['Alice' 24 70000] # ['Bob' 27 80000] # ['Charlie' 22 65000]]
  8. import pandas as pd

    data = {'col1': [1, 2], 'col2': [3, 4]}
    df = pd.DataFrame(data)
    df_transpose = df.transpose()
    df_transpose_attr = df.T
    print("Dataframe:")
    print(df)
    print("Transpose using transpose() method:")
    print(df_transpose)
    print("Transpose using 'T'")
    print(df_transpose_attr)
         






Dataframe attributes list Attributes of dataframe Class 12 Pandas DataFrame attributes list Dataframe attributes pdf Dataframe attributes examples Pandas attributes and methods DataFrame' object has no attribute 'append Which attribute of data frame is used to retrieve its shape

Post a Comment

0 Comments