Class 12 DataFrame accessing using all method-------- Important for board exam

 Class 12 Accessing DataFrame All methods 

First, let's create a sample DataFrame:

import pandas as pd # Sample data data = { 'Year': [2020, 2021, 2022, 2023], 'Subject': ['Math', 'Physics', 'Chemistry', 'Biology'], 'Question': [ 'Find the derivative of the function f(x) = x^2 + 2x + 1.', 'Explain the concept of quantum mechanics and its applications.', 'Describe the process of electrolysis and its uses.', 'Discuss the structure and function of the human respiratory system.' ] } # Creating DataFrame df = pd.DataFrame(data)

Accessing and Showing DataFrame Columns

  1. Display All Columns

    # Display all column names print(df.columns)
  2. Access a Single Column


    # Access the 'Year' column print(df['Year'])
  3. Access Multiple Columns

    # Access the 'Year' and 'Subject' columns print(df[['Year', 'Subject']])
  4. Access Columns Using Dot Notation


    # Access the 'Subject' column using dot notation print(df.Subject)
  5. Get Data Types of Columns

         # Display the data types of each column
       print(df.dtypes)





Output

Executing these commands will produce the following outputs:

  1. Display All Columns


    Index(['Year', 'Subject', 'Question'], dtype='object')
  2. Access a Single Column


    0 2020 1 2021 2 2022 3 2023 Name: Year, dtype: int64
  3. Access Multiple Columns

    Year Subject 0 2020 Math 1 2021 Physics 2 2022 Chemistry 3 2023 Biology
  4. Access Columns Using Dot Notation


    0 Math 1 Physics 2 Chemistry 3 Biology Name: Subject, dtype: object
  5. Get Data Types of Columns

  6. Year int64
    Subject object Question object dtype: object


Post a Comment

0 Comments