0% found this document useful (0 votes)
10 views4 pages

Python SQL Pandas Visualization Notes

The document provides study notes on Python and SQL, focusing on data handling with Pandas, data visualization using Matplotlib, and database queries with SQL. It covers key concepts, functions, and examples for each topic, including Series and DataFrame in Pandas, various types of charts in Matplotlib, and SQL commands for data management. Important questions and Python/SQL code snippets are included to aid understanding and application.

Uploaded by

masifakrami
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)
10 views4 pages

Python SQL Pandas Visualization Notes

The document provides study notes on Python and SQL, focusing on data handling with Pandas, data visualization using Matplotlib, and database queries with SQL. It covers key concepts, functions, and examples for each topic, including Series and DataFrame in Pandas, various types of charts in Matplotlib, and SQL commands for data management. Important questions and Python/SQL code snippets are included to aid understanding and application.

Uploaded by

masifakrami
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

Python & SQL Study Notes – Pandas | Visualization

| Database Queries

1■■ Data Handling Using Pandas – I

Summary:
Pandas is a Python library for data analysis and manipulation. It provides two main structures:
Series (1D) and DataFrame (2D). Pandas helps read, write, merge, and analyze tabular data
easily.

Key Points:
• Series – One-dimensional labeled array (like Excel column). Created using [Link]().
• DataFrame – Two-dimensional data structure with rows & columns.
• Access data using loc[], iloc[], slicing, and indexing.
• Add/Delete columns using assignment, del, pop(), or drop().
• Boolean indexing filters data using conditions (e.g., df[df['marks']>50]).
• File handling: pd.read_csv() and df.to_csv() for reading/writing CSV files.

Important Questions:
– Define Pandas and its two data structures.
– Differentiate between Series and DataFrame.
– Explain loc() and iloc() with examples.
– How do you add and delete columns in a DataFrame?
– What is Boolean Indexing?
– Explain different join types in Pandas.
– How to import/export CSV files using Pandas?

Python Programs:
Create Series from list:
import pandas as pd
s = [Link]([10,20,30,40])
print(s)

Create DataFrame from dictionary:


import pandas as pd
data = {'Name':['Ravi','Asha'],'Marks':[85,90]}
df = [Link](data)
print(df)

Select rows using loc:


print([Link][0])

Add & Delete column:


df['Grade']=['A','A+']
del df['Grade']

Read and Write CSV:


df.to_csv('[Link]')
new_df = pd.read_csv('[Link]')

2■■ Data Visualization

Summary:
Data Visualization helps in understanding data using graphs and charts. Python uses Matplotlib's
pyplot module to create 2D visuals like Line, Bar, Pie, Histogram, Box, and Scatter charts.

Key Points:
• Line Graph – [Link](x, y, color, linewidth, linestyle).
• Bar Graph – [Link](x, y) or [Link](x, y) for horizontal.
• Pie Chart – [Link](values, labels, explode, autopct, shadow).
• Histogram – [Link](data, bins, rwidth, edgecolor) shows frequency.
• Box Plot – [Link](data) shows distribution with quartiles.
• Scatter Plot – [Link](x, y, color, marker).
• Save Plot – [Link]('[Link]').

Important Questions:
– What is data visualization and why is it important?
– Explain Matplotlib and Pyplot module.
– Differentiate Bar chart and Histogram.
– What is the use of explode, autopct, and shadow in pie charts?
– Define IQR in Box plot and its formula.
– Explain Scatter plot markers with examples.

Python Programs:
Line Graph:
import [Link] as plt
x=[1,2,3,4]
y=[2,4,6,8]
[Link](x,y,color='red',linewidth=2)
[Link]()

Bar Graph:
[Link](['A','B','C'],[10,20,15],color='blue')
[Link]()

Pie Chart:
[Link]([20,30,50],labels=['Math','Sci','Eng'],autopct='%.1f%%',explode=[0,0.1,0],shadow=True)
[Link]()

Histogram:
data=[10,20,30,20,10,40]
[Link](data,bins=4,rwidth=0.6,edgecolor='black')
[Link]()

Box Plot:
[Link]([10,20,30,40,50],patch_artist=True,notch=True)
[Link]()

Scatter Plot:
x=[1,2,3,4]
y=[5,4,6,7]
[Link](x,y,marker='o',color='green')
[Link]()

3■■ Database Query Using SQL

Summary:
SQL (Structured Query Language) manages data in relational databases. It allows sorting, filtering,
grouping, and performing calculations on data using aggregate functions.

Key Points:
• ORDER BY – Sorts data ascending or descending.
• Aggregate Functions – SUM(), AVG(), COUNT(), MAX(), MIN().
• GROUP BY – Divides data into logical groups for calculations.
• HAVING – Filters grouped data (used after GROUP BY).
• String Functions – LOWER(), UPPER(), TRIM(), CONCAT(), LENGTH().
• Math Functions – ROUND(), POWER(), SQRT(), MOD().
• Date & Time Functions – CURDATE(), NOW(), DAYNAME(), MONTH(), YEAR().

Important Questions:
– What is ORDER BY clause? Give example.
– Explain five aggregate functions in SQL.
– Difference between COUNT(*) and COUNT(column).
– What is GROUP BY? Write its syntax.
– Explain HAVING clause with example.
– List 5 string, 5 mathematical, and 5 date/time functions.

SQL Query Examples:


Sorting Records:
SELECT * FROM emp ORDER BY salary DESC;
Aggregate Functions:
SELECT SUM(salary), AVG(salary), MAX(salary), MIN(salary) FROM emp;

GROUP BY with HAVING:


SELECT dept, COUNT(*) FROM emp GROUP BY dept HAVING COUNT(*)>2;

String Function:
SELECT CONCAT(name, ' works in ', dept) FROM emp;

Date Function:
SELECT name, CURDATE(), DAYNAME(CURDATE()) FROM emp;

Common questions

Powered by AI

The IQR, or Interquartile Range, is a measure of statistical dispersion in box plots that indicates the range within which the middle 50% of the data falls, calculated as the difference between the 75th percentile (Q3) and the 25th percentile (Q1). It is significant because it represents the central tendency of the data set while minimizing the influence of outliers or extreme values . Analyzing the IQR helps in understanding the spread and variability of the central data points, providing critical insights into distribution symmetry, data skewness, and potential outliers, which can all impact the interpretations of data tendencies and structures .

The HAVING clause in SQL is used to filter records that meet certain conditions, specifically after data has been grouped by the GROUP BY clause. This allows analysts to impose conditions on grouped row sets rather than on individual rows, enhancing the query's analytical power . For instance, in the query SELECT dept, COUNT(*) FROM emp GROUP BY dept HAVING COUNT(*)>2;, the HAVING clause filters out departments with less than three employees, making it possible to focus on more substantial groups . This capability is pivotal for refining results based on aggregated data, which the WHERE clause cannot achieve since it is applied before data grouping .

Aggregate functions in SQL such as SUM(), AVG(), COUNT(), MAX(), and MIN() provide a means to perform calculations on a set of values to return a single scalar value, which enhances data analysis by summarizing large volumes of data efficiently . For example, SUM() calculates the total of a numeric column, AVG() provides the average value, COUNT() tallies the number of rows that match a specified criteria, MAX() finds the highest value, and MIN() identifies the smallest value. These functions are fundamental in generating insightful summaries and performing statistical analyses on database tables .

The ORDER BY clause in SQL sorts the result set returned by a query in either ascending or descending order, based on one or more columns. It is commonly used to organize retrieved data meaningfully, enhancing its readability and aiding decision-making processes . For example, SELECT * FROM emp ORDER BY salary DESC; sorts employees by their salary in descending order, making it easier to identify the highest earners . This functionality is vital in scenarios requiring sorted data, such as generating reports or viewing ranked lists .

A Pandas Series is a one-dimensional labeled array, similar to an Excel column, which is primarily used for storing data of a similar type or performing operations on a single category of data . In contrast, a DataFrame is a two-dimensional labeled data structure with columns of potentially different types, akin to an Excel spreadsheet . This makes DataFrames suitable for more complex data manipulation tasks involving multiple variables or dimensions compared to Series . The choice between using a Series and a DataFrame largely depends on whether the dataset being analyzed requires handling multi-dimensional data or is restricted to a single dimension .

String functions in SQL greatly enhance data manipulation by allowing for efficient text processing and transformation, critical for managing and querying text-heavy databases. Examples of common functions include LOWER() which converts strings to lowercase, UPPER() for converting to uppercase, TRIM() which removes leading and trailing spaces, CONCAT() for appending strings, and LENGTH() to find a string's length . These functions enable SQL users to clean and modify text data, facilitate text-based analyses, create customized outputs, and ensure harmonized data formatting across the database .

Boolean Indexing in Pandas allows the filtration of data by applying conditions directly on DataFrames or Series. This technique utilizes boolean values (True or False) to filter data that satisfies a specified condition . For instance, if you have a DataFrame 'df' and you want to filter rows where the 'marks' column is greater than 50, you can use the expression df[df['marks'] > 50]. This creates a new DataFrame containing only the rows where the condition is True, enabling efficient data analysis based on specific criteria .

Matplotlib, specifically its pyplot module, is a Python library used for creating static, interactive, and animated visualizations, significantly enhancing data understanding through graphical representations . It supports various chart types, including line graphs, bar charts, pie charts, histograms, box plots, and scatter plots, each suited for different data insights . For example, line graphs are ideal for depicting trends, while pie charts are used for illustrating proportionate relationships between parts of a whole . Visualization helps in identifying patterns, trends, and outliers in data, making it a powerful tool for data analysis .

The primary purpose of the GROUP BY clause in SQL is to aggregate data into logical groups based on one or more columns before performing aggregate calculations, allowing for more focused analyses . It differs from directly using aggregate functions as it organizes the dataset into subsets, enabling the application of functions like AVG(), COUNT(), or SUM() within each group rather than across the entire dataset. For example, SELECT dept, COUNT(*) FROM emp GROUP BY dept; counts employees within each department independently, offering granular insights into the composition of departments .

Bar charts are preferred when comparing discrete categories or groups, as they effectively represent categorical data through separated bars, making them ideal for visualizing nominal or ordinal data . Histograms, on the other hand, are suitable for displaying the distribution of numerical data and the frequency of data within certain ranges or bins, often utilized in visualizing continuous data . Choosing a bar chart over a histogram is advantageous when emphasis is on clear categorization and comparison among distinct groups rather than showing distribution trends or frequency of continuous data ranges .

You might also like