Page 100 - Informatics_Practices_Fliipbook_Class12
P. 100
Now, let's say you want to find the average score for each student across different subjects. You can use groupby to achieve
this:
averageScores = df.groupby('Student')['Score'].mean()
print(averageScores)
Student
Alice 84.333333
Bob 85.666667
Name: Score, dtype: float64
In this example, groupby('Student') splits the DataFrame into groups based on the 'Student' column, and then
['Score'].mean() calculates the mean score for each student. The result is a new Series where the index is the unique
student names, and the values are the average scores for each student.
16. Write the steps required to read data from a MySQL database to a DataFrame.
Ans. To read data from a MySQL database to a dataframe follow these steps:
a. Install Required Packages:
pip install pymysql
b. Import Libraries:
import pandas as pd
import pymysql
c. Establish Database Connection:
Create a connection to the MySQL database using the mysql.connector.connect() function. Provide the necessary
connection parameters such as host, user, password, and database.
connection = pymysql.connect(
host='your_host',
user='your_user',
password='your_password',
database='your_database'
)
d. Execute SQL Query:
Use the pd.read_sql() function to execute an SQL query and read the result directly into a Pandas DataFrame.
query = "SELECT * FROM your_table"
df = pd.read_sql(query, connection)
Replace 'your_table' with the actual table name you want to query.
e. Close the Connection:
After reading the data, close the database connection to free up resources.
connection.close()
17. Explain the importance of reshaping of data with an example.
Ans. Reshaping data is crucial for analysis, as it transforms raw data into a structured format suitable for specific tasks.
18. Why estimation is an important concept in data analysis?
Ans. Estimation is an important concept in data analysis to handle the missing values.
The estimation refers to approximations to a nearby value or average value or zero.
Estimation also helps in predicting or making inferences about the the population that will be a good approximation of
actual results based on the sample data.
86 Touchpad Informatics Practices-XII

