Unit 2 2
Unit 2 2
1. Correlation:
Shows how strongly two variables are related and whether the relationship is positive or negative.
o +1: Perfect positive correlation (when one increases, the other increases)
o -1: Perfect negative correlation (when one increases, the other decreases)
Example:
import pandas as pd
data = {
df = pd.DataFrame(data)
print(df.corr())
Output:
StudyHours Scores
2. Covariance:
Measures how two variables vary together (but doesn’t scale the result like correlation).
Example:
print(df.cov())
Output:
StudyHours Scores
import pandas as pd
print(obj.unique())
Output:
print(obj.value_counts())
Output:
a 2
b 2
c 2
d 1
print(mask)
print(obj[mask])
Output:
0 True
1 False
2 True
3 True
4 False
5 False
6 True
0 a
2 a
3 c
6 c
6.1 Reading and Writing Data in Text Format (from pandas)
Example:
import pandas as pd
df = pd.read_csv("sample_data.csv")
print(df.head())
Example:
df.to_csv("output.csv", index=False)
Small Example
import pandas as pd
df = pd.DataFrame(data)
# Save to CSV
df.to_csv('students.csv', index=False)
# Read it back
df2 = pd.read_csv('students.csv')
print(df2)
Output:
Name Age
0 Shahul 21
1 Ravi 22
6.2 Interacting with Web APIs and Databases
Web APIs return data (usually in JSON format) over the internet. We can fetch and convert this data into a pandas
DataFrame using the requests library.
import requests
import pandas as pd
url = "https://jsonplaceholder.typicode.com/posts"
response = requests.get(url)
data = response.json()
df = pd.DataFrame(data)
print(df.head())
This fetches dummy blog post data and shows the first 5 entries.
Python can connect to many databases like SQLite, MySQL, PostgreSQL using libraries like sqlite3 or SQLAlchemy.
import sqlite3
import pandas as pd
conn = sqlite3.connect('mydata.db')
conn.commit()
print(df)
conn.close()