0% found this document useful (0 votes)
20 views7 pages

Pandas Practical Exercises Guide

The document outlines various practical exercises using Pandas, including creating Series and DataFrames from different data structures, displaying and manipulating data, and performing operations like filtering and adding columns. It also includes SQL queries for managing teacher data in a database. Additionally, it mentions creating visualizations using Matplotlib and working with CSV files.

Uploaded by

aditisingh8thd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

Pandas Practical Exercises Guide

The document outlines various practical exercises using Pandas, including creating Series and DataFrames from different data structures, displaying and manipulating data, and performing operations like filtering and adding columns. It also includes SQL queries for managing teacher data in a database. Additionally, it mentions creating visualizations using Matplotlib and working with CSV files.

Uploaded by

aditisingh8thd
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Pandas Practical File

Practical 1:- Create a Series from dictionary


RollNo 100
Name Sneha Sahoo
Index Class XII A
DOB 2005-05-20
Tmarks 489
Grade A
Practical 2:- Create a Series from ndarray.
Amit 456
Sneha 478
index Manya 467
Pari 477
Lavanya 489
Abhay 477
Practical 3:- Create a Series, print all the element that are above the 75th percentile.

[ 15, 16, 18, 27, 29, 32, 36]


Practical 4:- Create a Data Frame from List of Dictionary.

Country Population Birth_rate Update_date


China 137975000 12.00 2016-08-11
India 1330780000 21.76 2016-08-11
USA 324882000 13.21 2016-08-11
Indonesia 260581000 18.84 2016-01-07
Brazil 206918000 18.43 2016-05-11

Practical 5 :- Create a Data Frame for examination results, display row labels, column labels, types of
each columns and the dimensions by using dictionary.

Maths Physics Chemistry IP English


Suyash 94 90 93 95 92
Amit 95 98 96 90 91
Ankur 98 90 89 92 94
Aditya 90 88 92 95 88
Akash 97 91 89 92 90

Practical 6 :- create a DataFrame called df.

Name Class English physics chemistry Maths IP


0 Advik 11 85 70 60 55 80
1 Rohit 12 65 75 65 65 75
2 Ram 11 75 58 68 45 57
3 Shailesh 12 50 65 65 65 45
4 Atharv 12 65 75 75 58 65
5 Nitesh 12 58 74 65 52 85
6 Kundan 11 70 62 52 52 80

i. Display Student name and Marks of Maths and IP subjects.


ii. Display the records of students of class 11.
iii. Display student name and marks of Physics for all the students
iv. Display the records of the students whose marks are less than 55 in Maths subject.

Practical 7:- Consider the given DataFrame ‘Genre’:


Type Code
0 Fiction F
1 Non Fiction NF
2 Drama D
3 Poetry P
Write suitable Python statements for the following:
i. Add a column called Num_Copies with the following data:
[300,290,450,760].
ii. Add a new genre of type ‘Folk Tale' having code as “FT” and 600 number of copies.
iii. Rename the column ‘Code’ to ‘Book_Code’
iv. delete second record of DataFrame ‘Genre’

Practical 8:- For the following DataFrame batsman:

B_No Name Score1 Score2


1 Sunil Pillai 90 80
2 Gaurav Sharma 65 45
3 Piyush Goel 70 90
4 Kartik Thakur 80 76
i. Add new column ‘Total’ to the DataFrame with values (addition of Score1 and Score2).
ii. Print the maximum value of ‘Score1’ and ‘Score2’ columns.

Practical 9: - Consider the following DataFrame , XII;

Name RollNo English Hindi Maths SSc Science


0 Zahid 1 55 45 87 67 70
1 Moin 2 67 65 90 87 56
2 Ishrat 3 78 76 98 90 78
3 Neha 4 98 87 78 45 87
4 Prateek 5 45 58 90 69 98
Write code to display the dataframe ‘XII’ row-wise.

Practical 10. Create dataframe from csv file using notepad.


Practical 11. Create dataframe from csv file using MS Excel.

Practical 11. Create dataframe from SQL table using sql Database.

Based on Topic Matplotlib Program


Program 12 write a Python script to display line chart of the below given data:
Program 13 :- Smile NGO participated in a 3 week cultural Mela. Using Pandas, they have stored in
Sales (In Rs.) made day wise for every week in a CSV file named “[Link]” as below:
Program 14 : write a Python script to display bar plot for the “[Link]” with column day on x
axis as show below:

Program 15 :
SQL Queries
Dear students first you create table
Table name – teacher and insert these records in MySQL software
Query 1 – Write a SQL Query to display the maximum salary of each department.
Query 2- Write a SQL query to display the minimum salary of each department.
Query 3- Write the SQL query to display the maximum salary of those department whose
department salary more than 90,000.
Query 4- write the SQL query to display the maximum salary whose salary between 90,000
and 110,000.
Query 5 – Write the SQL query to display the minimum salary of each department whose
salary is less than 85000.
Query 6 – Write a SQL query to display average salary of each department.
Query 7 – Write a SQL query to display average salary of each department whose average
salary is more than 100,000.
Query 8- Write a SQL Query to display the total salary of each department.
Query 9- Write a SQL Query to count the employee of each department.
Query 10 – Write a SQL Query to display teacher name in lower case along with column
teacher number and salary.
Query 11 – Write a SQL query to display teacher name in upper case along with column
teacher number and date of joining.
Query 12 – Write a SQL Query to display first seven letter in teacher name along with
column teacher number and salary.
Query 13 - Write a SQL Query to display last six letters in teacher name along with column
teacher number and salary.
Query 14 - Write a SQL Query to display of first occurrence of substring “the” in the
teacher address along with teacher name and teacher address.
Query 15 - Write a SQL Query to display 7 characters extracted from 7th left character from
the teacher’s name along with teacher name and teacher address.
Query 16 - Write a SQL Query to display total number of characters in teacher’s name ess
along with teacher number, teacher name and department number.
Query 17 – Write a SQL Query to display month name from date of joining along with
column teacher name and department number.

Common questions

Powered by AI

Displaying DataFrames row-wise provides detailed insights into each record, promoting data verification and granular analysis. It involves looping through the DataFrame rows, often using `iterrows()` in Pandas. While beneficial for detailed scrutiny, challenges include increased computational load with large datasets and potential inefficiencies in execution time compared to vectorized operations. Careful implementation balances detail retrieval and performance .

Integrating SQL and Pandas leverages the querying power of SQL and the analytical capabilities of Pandas. For example, SQL can extract structured data from databases using queries, which can then be analyzed and modified in Pandas using DataFrames. Tasks such as data aggregation (`GROUP BY` in SQL, `.groupby()` in Pandas) or filtering specific conditions can be seamlessly performed using both. This synergy allows handling complex data manipulation tasks efficiently by utilizing SQL for data extraction and Pandas for detailed analysis, covering a wide range of data requirements .

Creating a DataFrame from a CSV file involves using `pd.read_csv('file_path')` in Pandas to load the data. Using Notepad or MS Excel is beneficial because these tools allow you to inspect and format the CSV file easily, ensuring data integrity and correct formatting before loading. Excel provides advanced functionalities like data validation and formatting, while Notepad offers a straightforward text-editing environment .

Creating a DataFrame from a list of dictionaries involves using the syntax `pd.DataFrame(list_of_dicts)`. Each dictionary in the list corresponds to a row, with keys as column headers. For example, using a dataset with country populations, each dictionary entry can represent a country's data, with keys like 'Country', 'Population', and 'Birth_rate'. This method is useful for structured data with defined attributes, allowing for easy manipulation and analysis of tabular data .

Column manipulation enhances DataFrame functionality by enabling customization and restructuring of data. For example, adding a column like 'Num_Copies' to a 'Genre' DataFrame allows tracking additional metrics without altering existing data. Renaming columns, such as changing 'Code' to 'Book_Code', improves clarity and consistency. Deleting unnecessary columns can declutter data representation, as deleting the second record from 'Genre'. These operations facilitate tailored data management and analysis .

Percentiles are significant in data analysis as they help understand the distribution and spread of data. To filter elements above the 75th percentile in a Pandas Series, you calculate the 75th percentile using `np.percentile(series, 75)` and then use boolean indexing to filter `series[series > percentile_value]`. This identification assists in finding outliers and understanding data variability .

A Pandas Series can be created from a dictionary using the syntax `pd.Series(dictionary)`, where the dictionary keys become the Series' index. This allows for easy lookup and management of data by specific values. Alternatively, a Series can be created from an ndarray using `pd.Series(ndarray)`, which is useful for handling homogeneous data with index values defined by the user. NDarrays provide efficient numerical computation advantages, whereas dictionaries offer flexible data indexing .

Visualization libraries like Matplotlib enhance sales data analysis by providing graphical representations that help identify trends and patterns quickly. For a cultural mela's sales data, line charts offer insights into daily sales fluctuations, revealing peak sale days. Bar plots can compare week-to-week performances effectively. These visual tools transform numeric data into an intuitive format, aiding stakeholders in making informed decisions about future events .

SQL queries utilize aggregation functions like MAX, MIN, and AVG to summarize salary data efficiently. For example, `SELECT MAX(salary) FROM teachers WHERE department.salary > 90000` retrieves the highest salary from departments exceeding a threshold. `MIN` finds the lowest salary, and `AVG` calculates the mean salary, helping in understanding salary distributions and differences across departments. These functions enable concise and powerful data analysis for decision-making .

To display specific columns and rows in a Pandas DataFrame, you can use DataFrame indexing and slicing methods. For instance, to display columns for student names and 'Maths' and 'IP', use `df[['Name', 'Maths', 'IP']]`. To retrieve records for students from class 11, employ a boolean condition like `df[df['Class'] == 11]`. This capability allows targeted data retrieval based on conditions .

You might also like