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
Display All Columns
# Display all column names print(df.columns)
Access a Single Column
# Access the 'Year' column print(df['Year'])
Access Multiple Columns
# Access the 'Year' and 'Subject' columns print(df[['Year', 'Subject']])
Access Columns Using Dot Notation
# Access the 'Subject' column using dot notation print(df.Subject)
Get Data Types of Columns
# Display the data types of each column
print(df.dtypes)
Output
Executing these commands will produce the following outputs:
Display All Columns
Index(['Year', 'Subject', 'Question'], dtype='object')
Access a Single Column
0 2020 1 2021 2 2022 3 2023 Name: Year, dtype: int64
Access Multiple Columns
Year Subject 0 2020 Math 1 2021 Physics 2 2022 Chemistry 3 2023 Biology
Access Columns Using Dot Notation
0 Math 1 Physics 2 Chemistry 3 Biology Name: Subject, dtype: object
Get Data Types of Columns
- Year int64
Subject object Question object dtype: object
0 Comments
Please do note create link post in comment section