0% found this document useful (0 votes)
4 views2 pages

Python Data Handling & Visualization Guide

The document covers data handling using the Pandas library in Python, detailing its key structures like Series and DataFrame, and methods for data manipulation. It also discusses data visualization techniques using Matplotlib, including various chart types and their syntax. Lastly, it introduces SQL for database querying, explaining essential functions and clauses such as ORDER BY, aggregate functions, and GROUP BY.

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)
4 views2 pages

Python Data Handling & Visualization Guide

The document covers data handling using the Pandas library in Python, detailing its key structures like Series and DataFrame, and methods for data manipulation. It also discusses data visualization techniques using Matplotlib, including various chart types and their syntax. Lastly, it introduces SQL for database querying, explaining essential functions and clauses such as ORDER BY, aggregate functions, and GROUP BY.

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 Data Handling, Visualization & SQL

Summary

1. Data Handling Using Pandas – I


Pandas is a Python library used for data analysis and manipulation. It provides two main data
structures: 1. Series – 1D labeled array (like a column) 2. DataFrame – 2D labeled table (like Excel)
Key Concepts:
• Series: One-dimensional, labeled, holds any data type. Created using [Link](data, index=...).
• DataFrame: Two-dimensional table, created from Series, lists, dictionaries, or NumPy arrays.
• Access data using loc[], iloc[], or slicing like df[2:5].
• Boolean Indexing selects rows based on conditions.
• Merging, joining, and concatenation combine data.
• CSV file handling: pd.read_csv() and df.to_csv() for import/export.
Important Questions:
– Define Pandas and its data structures.
– What is the difference between Series and DataFrame?
– Explain loc(), iloc(), and slicing in Pandas.
– What is Boolean indexing? Give example.
– Explain different join types in Pandas.
– How do you read and write CSV files in Pandas?

2. Data Visualization
Data visualization represents data in graphical form for better understanding. Python uses the
Matplotlib library for visualization, mainly its submodule Pyplot.
• Line Graph: [Link](x, y, color, linewidth, linestyle) – shows trends.
• Bar Graph: [Link](x, y) – compares categories; [Link]() for horizontal bars.
• Pie Chart: [Link](values, labels, colors, explode, autopct, shadow).
• Histogram: [Link](data, bins, rwidth, edgecolor) – shows distribution.
• Box Plot: [Link](data) – shows median, quartiles, and outliers.
• Scatter Plot: [Link](x, y, color, marker) – shows relation between variables.
• Save Plot: [Link]('[Link]').
Important Questions:
– What is data visualization?
– Explain line, bar, and pie charts with syntax.
– What is the difference between bar graph and histogram?
– Define IQR and explain box plot components.
– Explain scatter plot with marker examples.
– How to save a plot in Matplotlib?

3. Database Query Using SQL


SQL (Structured Query Language) is used to manage and query data in relational databases.
• Sorting: ORDER BY clause – SELECT * FROM emp ORDER BY salary DESC;
• Aggregate Functions: SUM(), AVG(), COUNT(), MAX(), MIN() – perform calculations on data.
• GROUP BY: Groups rows and applies aggregate functions on each group.
• HAVING: Filters groups formed by GROUP BY clause.
• 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.
– List and explain aggregate functions in SQL.
– Difference between COUNT(*) and COUNT(column).
– Explain GROUP BY with example.
– What is the purpose of HAVING clause?
– List 5 string, 5 math, and 5 date/time functions with examples.

Common questions

Powered by AI

The HAVING clause is used in SQL to filter records that work on aggregated data after the GROUP BY clause has been applied. It differs from WHERE because WHERE acts on rows before aggregation, while HAVING filters results after an aggregation has been applied. For instance, SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 10 will show only departments with more than 10 employees.

Series in Pandas is a one-dimensional labeled array capable of holding any data type, similar to a column in a table. In contrast, a DataFrame is a two-dimensional labeled table that consists of multiple Series and can be compared to an entire spreadsheet or SQL table. While a Series is useful for single-dimensional data, DataFrames are designed for multi-dimensional data analysis and manipulation.

Pandas provides several types of joins: 'inner', 'outer', 'left', and 'right'. An 'inner' join returns only the rows with keys present in both datasets, 'outer' join returns all rows from both datasets, filling in NaNs for missing matches. A 'left' join returns all rows from the left dataset with matching rows from the right dataset. Conversely, a 'right' join returns all rows from the right dataset with matching rows from the left dataset.

A bar graph is used for comparing different categories, and each bar represents a different category. A histogram, however, is used for showing the distribution of a numerical dataset by dividing the data into 'bins' of equal width. Bar graphs are ideal for categorical data, while histograms are best for continuous, numerical data.

A scatter plot in Matplotlib visualizes the relationship between two numerical variables by displaying data points on a two-axis graph. The syntax is plt.scatter(x, y, color='blue', marker='o'), where 'x' and 'y' are arrays of data. Different markers such as 'o' for circle, '^' for triangle, and 's' for square can be used based on visualization requirements.

In Pandas, reading CSV files is handled using the pd.read_csv() function, which loads data from a CSV file into a DataFrame, making it easy to explore and manipulate. Similarly, df.to_csv('filename.csv') exports a DataFrame to a CSV file, enabling data storage and sharing. These functions are essential for data import and export in data analysis workflows.

The GROUP BY clause in SQL is used to group rows that have the same values in specified columns into summary rows, like 'counting the number of customers in each country'. It is often used with aggregate functions such as SUM(), COUNT(), AVG(), etc., to perform calculations on each group rather than the entire dataset. For example, SELECT department, SUM(salary) FROM employees GROUP BY department; calculates the total salary for each department.

A box plot displays the distribution of data based on five number summary: minimum, first quartile (Q1), median, third quartile (Q3), and maximum. It identifies outliers and variation in a dataset. The box represents the interquartile range (IQR), while 'whiskers' extend to the smallest and largest observations within 1.5 * IQR from the quartiles. Outliers are plotted as individual points.

Boolean indexing in Pandas allows for selecting data based on the results of applying conditions to the dataset. For example, if you have a DataFrame 'df' with a column 'age', you can select rows where the age is greater than 30 using: df[df['age'] > 30]. This will return all rows where the condition is true.

Aggregate functions in SQL perform calculations on a set of values and return a single value. SUM() calculates the total, e.g., SELECT SUM(salary) FROM employees; for the total salary. AVG() finds the average, COUNT() counts the number of entries, MAX() returns the maximum value, and MIN() gives the minimum. These functions are crucial for data summary and reporting. For instance, SELECT AVG(age) FROM users; calculates the average age of users.

You might also like