0% found this document useful (0 votes)
12 views16 pages

Pandas DataFrame Basics in Python

Chapter 2 covers data handling using Pandas DataFrame, explaining its structure, creation from dictionaries or CSV files, and methods for data manipulation, including retrieving subsets and descriptive statistics. It also introduces data visualization techniques using Matplotlib, detailing how to create line graphs, bar graphs, and histograms to represent data effectively. Additionally, the document discusses digital responsibilities, intellectual property rights, cyber threats, and the importance of managing e-waste.

Uploaded by

karunakar.kavi
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)
12 views16 pages

Pandas DataFrame Basics in Python

Chapter 2 covers data handling using Pandas DataFrame, explaining its structure, creation from dictionaries or CSV files, and methods for data manipulation, including retrieving subsets and descriptive statistics. It also introduces data visualization techniques using Matplotlib, detailing how to create line graphs, bar graphs, and histograms to represent data effectively. Additionally, the document discusses digital responsibilities, intellectual property rights, cyber threats, and the importance of managing e-waste.

Uploaded by

karunakar.kavi
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

Chapter 2: Data Handling using Pandas DataFrame

Imagine you have a giant table of data, like your class's report card with names, subjects,
and marks. A Pandas DataFrame is like a super-powered digital version of that table,
which you can control and analyze using Python.

2.1 Pandas DataFrame

What is it?
Think of a DataFrame as a container for your data that looks exactly like a table in Excel
or Google Sheets. It has rows and columns.

 Columns: Represent different types of information (e.g., 'Name', 'Roll No', 'Marks').
 Rows: Represent individual records (e.g., data for each student).

Simple Example:

Name Roll No Marks

Alice 1 95

Bob 2 88

Charlie 3 92

This table is a DataFrame.

2.2 Creation of Pandas Dataframe

How do we create this table in code?


You can create a DataFrame from different sources. The most common way is from a
Python dictionary.
Code Example:
python
import pandas as pd

# Create a dictionary
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Roll No': [1, 2, 3],
'Marks': [95, 88, 92]
}

# Convert the dictionary into a DataFrame


df = [Link](data)

# Display the DataFrame


print(df)

2.3 Reading from a CSV File

What is a CSV file?


A CSV (Comma-Separated Values) file is a simple text file where data is separated by
commas. It's the most common way to store and share table data.

Why read from a CSV?


Instead of typing all the data manually in your code, you can load it directly from a file
(e.g., [Link]).

Code Example:
python
import pandas as pd

# Read the CSV file


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

# Display the first few rows


print([Link]())
2.4 Dimensions of a DataFrame

How big is your data?


This tells you the size of your table.

 [Link]: Gives you the number of (rows, columns).

o Example: If [Link] returns (50, 5), it means your table has 50 student records and 5
pieces of information about each (like Name, Roll No, etc.).

2.5 Summary Information about a DataFrame

Getting a quick overview.


This is like a "data health report."

 [Link](): Shows the data type of each column (e.g., text, numbers), how many non-
empty values there are, and how much memory it's using.
 [Link](n): Shows the first n rows of the table. Useful for peeking at the data.
 [Link](n): Shows the last n rows.

2.6 Retrieving Subset of Data - Indexing and Slicing

Picking out specific parts of your table.


This is like highlighting only the rows or columns you are interested in.

 Select a Single Column:

python

names = df['Name'] # Gets the entire 'Name' column

 Select Multiple Columns:


python
subset = df[['Name', 'Marks']] # Gets only the Name and Marks columns

 Select Rows by Index (Slicing):


python

first_5_students = df[0:5] # Gets rows from index 0 to 4 (first 5 rows)

2.7 Descriptive Statistics

Getting the "story" of your numbers.


This gives you a mathematical summary of the numerical columns in your data.

 [Link](): This one command gives you:

o Count: How many values are there?


o Mean: The average.
o Std: Standard Deviation (how spread out the numbers are).
o Min/Max: The smallest and largest value.
o 25%, 50%, 75%: Percentiles (e.g., 50% is the median).

If the 'Marks' column has values [95, 88, 92], describe() will tell you the average mark is
91.66, the highest is 95, etc.

2.8 Data Manipulation

Cleaning and changing your data.


Real-world data is often messy. This is about fixing it and making it useful.

 Handling Missing Data:

python

[Link]() # Removes rows with empty cells


[Link](0) # Replaces empty cells with 0

 Creating a New Column:


python

df['Percentage'] = (df['Marks'] / 100) * 100 # Creates a new column

2.9 Writing to a CSV File

Saving your work.


After you've cleaned or analyzed your data, you can save the modified DataFrame back
to a CSV file to use later.

Code Example:
python
df.to_csv('updated_students.csv', index=False)
# The 'index=False' means don't save the row numbers.

2.10 Grouping and Aggregation

"Group By" and "Summarize"


This is a powerful tool to analyze data by categories.

Example Scenario: You have sales data and want to know the total sales for each
salesperson.

 Step 1: Group by the 'Salesperson' column.


 Step 2: Apply a function like sum() on the 'Sales' column.

Code Example:
python
# Suppose df has 'Name', 'Subject', 'Marks'
# To find the average marks in each subject:
subject_wise_avg = [Link]('Subject')['Marks'].mean()
print(subject_wise_avg)

This will output something like:


text
Subject
Maths 85
Science 90
English 78

It grouped all rows by 'Subject' and then calculated the mean of the 'Marks' for each
group.

Unit 3: Data Visualization

What is Data Visualization?


Imagine you have a big table of numbers. It can be hard to see patterns, trends, or
comparisons just by looking at the raw data. Data Visualization is the art of
representing this data in a graphical or pictorial format. It turns numbers into charts and
graphs, making the data easier to understand and interpret.

Think of it like this: Which is easier to understand?

 A list of monthly sales numbers for a year?


 Or a line graph that shows how sales went up and down over the year?

The graph is almost always easier! We use the Matplotlib library in Python to create
these graphs.

3.1 Displaying the Line Graph


What is a Line Graph?
A line graph uses points connected by lines to show how something changes over time
or ordered categories. It's perfect for tracking trends.

When to use it?

 To track changes over time (e.g., temperature over a week, stock prices over a year).
 To compare trends for different groups (e.g., marks of two students over five tests).

How to create it?


We use [Link](x, y) where:

 x is the list of values for the horizontal axis (e.g., Months, Days).

 y is the list of corresponding values for the vertical axis (e.g., Sales, Temperature).

Code Example: Tracking Monthly Sales


python
import [Link] as plt

# Data
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
sales = [100, 120, 90, 150, 200, 180]

# Create the line graph


[Link](months, sales)

# Add labels and title (Very Important!)


[Link]("Monthly Sales for 2024")
[Link]("Months")
[Link]("Sales (in $1000s)")

# Display the graph


[Link]()

What you'll see: A line going from January to June, showing how sales fluctuated.
3.2 Bar Graph

What is a Bar Graph?


A bar graph uses rectangular bars of different heights (or lengths) to represent data. The
height of the bar is proportional to the value it represents.

When to use it?

 To compare quantities of different categories (e.g., sales of different products,


population of different cities).
 When the data is discrete (distinct categories).

How to create it?


We use [Link](x, height) where:

 x is the list of category names.

 height is the list of values for each category.

Code Example: Comparing Product Sales


python
import [Link] as plt

# Data
products = ['Laptop', 'Tablet', 'Phone', 'Headphones']
units_sold = [150, 200, 350, 400]

# Create the bar graph


[Link](products, units_sold)

# Add labels and title


[Link]("Product Sales Comparison")
[Link]("Products")
[Link]("Units Sold")
# Display the graph
[Link]()

What you'll see: Four bars of different heights, making it easy to see which product
sold the most (Headphones, in this case).

3.3 Histogram

What is a Histogram?
A histogram looks similar to a bar graph, but it is used for continuous numerical data.
It groups numbers into ranges (called "bins") and shows how many data points fall into
each range.

When to use it?

 To show the distribution of a dataset.


 To understand the spread and shape of the data (e.g., Are most students scoring
average marks?).

Key Difference from Bar Graph:

 Bar Graph: Spaces between bars. Used for categories (Laptop, Phone).
 Histogram: No spaces between bars. Used for number ranges (0-10 marks, 11-20
marks).

How to create it?


We use [Link](data, bins) where:

 data is a list of all the individual values.

 bins define the ranges (e.g., bins=5 will create 5 equal intervals).

Code Example: Distribution of Student Marks


python
import [Link] as plt

# Data: Marks of 30 students in an exam


marks = [55, 62, 71, 85, 90, 45, 68, 72, 78, 81, 52, 65, 70, 88, 92, 58, 64, 75, 80,
85, 48, 61, 69, 77, 83, 95, 50, 66, 74, 79]

# Create the histogram


[Link](marks, bins=5, edgecolor='black') # edgecolor adds a border to bars

# Add labels and title


[Link]("Distribution of Student Marks")
[Link]("Marks Range")
[Link]("Number of Students")

# Display the graph


[Link]()

What you'll see: A chart with 5 connected bars. You can quickly tell if most students
scored high, low, or in the middle. For example, if the tallest bar is in the 70-80 range, it
means the largest number of students scored average marks.

Summary & Key Differences

Graph Type Best For Python Function

Line Graph Showing trends over time [Link](x, y)

Bar Graph Comparing different categories [Link](x, height)

Histogram Showing distribution of numerical data [Link](data, bins)

Pro-Tip: Always remember to add a title, x-label, and y-label to your graphs. A graph
without labels is like a book without a title—it's confusing!
By mastering these three types of graphs, you can tell powerful stories with your data
and make your analysis much more impactful.

Unit 4: Societal Impacts

Part 1: Digital Life and Responsibilities

1. Digital Footprint

What is it?
Your digital footprint is the trail of data you leave behind every time you use the
internet. It's like walking on a wet beach—your footprints are left behind.

 Active Footprint: Data you intentionally leave online (e.g., posting on social
media, sending emails).
 Passive Footprint: Data collected without your direct input (e.g., websites
tracking your visit, apps collecting your location).

Why does it matter?

 Permanent Record: Things you post online can be permanent, even if you delete
them.
 Future Impact: Colleges and employers often check applicants' digital footprints.
 Privacy: A large footprint can make you vulnerable to identity theft.

Takeaway: Think before you click! Be mindful of what you share online.
2. Net and Communication Etiquettes

What is it?
The dos and don'ts of behaving politely and respectfully online. Just like in real life,
good manners are important.

 Be Respectful: Don't use abusive language. Respect others' opinions.


 Be Clear: Use proper language and avoid excessive slang in formal
communication.
 Respect Privacy: Don't share others' personal information or private messages
without permission.
 Avoid Spamming: Don't send unsolicited messages or forward chain emails.

Takeaway: The golden rule: Treat others online as you would like to be treated in
person.

3. Data Protection

What is it?
The practice of safeguarding important information from corruption, compromise, or
loss.

 Personal Data: Your name, address, phone number, Aadhaar number, bank
details, etc.
 How to Protect it?

o Use strong, unique passwords.


o Enable two-factor authentication (2FA).
o Be cautious about what information you share on websites and apps.
o Check privacy settings on social media.
Takeaway: Your personal data is valuable. Protect it like you protect your wallet.

Part 2: Intellectual Property and Software

4. Intellectual Property Rights (IPR)

What is it?
IPR are legal rights given to creators for their original work, like an invention, a book,
music, or software. It means you can't just copy someone else's hard work.

5. Plagiarism

What is it?
The act of using someone else's work (ideas, words, code) and presenting it as your
own. It is cheating and unethical.

 Example: Copying a paragraph from a website for your school project without
giving credit to the original author.

6. Licensing, Copyright, and FOSS

 Copyright: A legal right that grants the creator sole ownership of their original
work (e.g., a movie, a song). Others cannot copy or distribute it without
permission.
 Software License: The rules that define how you can use a piece of software.
 Free and Open Source Software (FOSS):

o Free: "Free as in freedom," not just "free of cost." You have the freedom to use,
study, modify, and share the software.
o Examples: Linux operating system, Firefox browser, LibreOffice.
Takeaway: Always respect creativity. Give credit where it's due and understand the
licenses of the software you use.

Part 3: Cyber Threats and Laws

7. Cybercrime

Criminal activities carried out using computers or the internet.

 Hacking: Gaining unauthorized access to a computer system or network.


 Phishing: A fraudulent attempt to steal your sensitive information (like passwords
or credit card details) by pretending to be a trustworthy entity in an email or
message.

o Example: You get an email that looks like it's from your bank, asking you to
"verify your account" by clicking a link.
 Cyber Bullying: Using digital technology to harass, threaten, or embarrass
someone.

o What to do? Do not respond. Save the evidence. Block the bully. Tell a trusted
adult.

8. Cyber Laws and Indian IT Act

 What are they? Laws designed to deal with cybercrime and electronic commerce.
 The Information Technology Act, 2000 (IT Act): The main law in India that
addresses cybercrime and electronic commerce. It defines punishments for
offenses like hacking, data theft, and publishing obscene material online.

Takeaway: The internet is not a lawless space. Cybercrimes are real crimes with real
consequences.
Part 4: Health and Environment

9. E-Waste: Hazards and Management

What is E-Waste?
Discarded electronic devices like old mobile phones, computers, TVs, and batteries.

 Hazards: E-waste contains toxic chemicals like lead and mercury. If not disposed
of properly, these can leak into the soil and water, causing serious health and
environmental problems.
 Management:

o Reduce: Don't change devices unnecessarily.


o Reuse: Donate old devices if they are working.
o Recycle: Always dispose of e-waste at authorized e-waste recycling centers.

Takeaway: Your old phone is not just garbage; it's hazardous e-waste. Dispose of it
responsibly.

10. Health Concerns Related to Technology

Excessive use of technology can lead to:

 Physical Health:

o Eye Strain: From staring at screens for too long (Use the 20-20-20 rule: every 20
minutes, look at something 20 feet away for 20 seconds).
o Neck and Back Pain: Due to poor posture (ergonomics).
o Sleep Problems: The blue light from screens can interfere with your sleep.
 Mental Health:
o Anxiety & Depression: Linked to social media pressure and cyberbullying.
o Addiction: Compulsive use of the internet, games, or social media.

Takeaway: Balance is key. Take regular breaks, maintain good posture, and ensure you
have plenty of offline activities and social interaction.

Unit 4 Summary: Key Takeaways

1. Be a Good Digital Citizen: Leave a positive digital footprint and practice


good etiquette.
2. Protect Yourself: Guard your personal data and be aware of cybercrimes like
phishing and hacking.
3. Respect Others' Work: Understand IPR, avoid plagiarism, and use software
legally.
4. Know the Law: The IT Act exists to protect you and hold cybercriminals
accountable.
5. Care for Your World: Recycle e-waste and be mindful of your health while using
technology.

This knowledge empowers you to navigate the digital world safely, ethically, and
responsibly

This response is AI-generated, for reference only.

You might also like