0% found this document useful (0 votes)
5 views3 pages

24 Assignment

The document outlines a series of steps for data manipulation using pandas in Python, including creating a DataFrame, filtering data, handling missing values, and performing group operations. It also explains key concepts such as the difference between Series and DataFrame, indexing methods, and how to manage duplicates. Additionally, it covers the purpose of vectorization and the use of correlation matrices in data analysis.

Uploaded by

Arkan IYI
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)
5 views3 pages

24 Assignment

The document outlines a series of steps for data manipulation using pandas in Python, including creating a DataFrame, filtering data, handling missing values, and performing group operations. It also explains key concepts such as the difference between Series and DataFrame, indexing methods, and how to manage duplicates. Additionally, it covers the purpose of vectorization and the use of correlation matrices in data analysis.

Uploaded by

Arkan IYI
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

# 1.

Create the DataFrame & display first 5 rows, last 3 rows, and shape

import pandas as pd
import numpy as np

data = {
'emp_id':[101,102,103,104,105,106,107,108],
'name':['Rohan','Priya','Mohit','Sana','Aditi','Rohan','Varun','Aditi'],
'age':[28,32,26,29,24,28,31,24],
'gender':['M','F','M','F','F','M','M','F'],
'department':['Sales','HR','IT','IT','Marketing','Sales','Finance','Market
ing'],
'salary':[55000,62000,48000,50000,45000,55000,70000,45000],
'score':[88,92,75,81,69,[Link],95,69],
'city':['Mumbai','Delhi','Bangalore','Mumbai','Chennai','Mumbai','Delhi','
Chennai']
}

df = [Link](data)

print([Link](5))
print([Link](3))
print([Link])
# 2. Select only name, age, salary into new DataFrame
df = df[['name','age','salary']]
print(df)

# 3 Filter employees age < 30 and salary > 50000


filter_df = df[(df['age']<30) & (df['salary'] > 50000)]
print(filter_df)

# 4. Replace missing score with mean score


mean_score = df['score'].mean()
df['score'] = df['score'].fillna(mean_score)

print(df)

# 5 Remove duplicate rows


before = [Link][0]
df = df.drop_duplicates()
after = [Link][0]

print("Before : ",before)
print("After : ",after)

# 6. Add new column bonus = 15% of salary


df['Bonus']= df['salary']*0.15
print(df)
# 7. Rename columns
df = [Link](columns={
'emp_id':'id',
'department':'dept',
'salary':'income'
})

print(df)
# 8. Group by department and calculate stats
grouped = [Link]('dept').agg({
'income':'mean',
'score':'max',
'name' :'count'
}).rename(columns={'name':'employee_count'})

print(grouped)

# 9. Sort by salary DESC and age ASC


sorted_df = df.sort_values(by=['income','age'], ascending=[False, True])
print(sorted_df)

# 10. Correlation matrix (age, salary, score)


corr = df[['age','income','score']].corr()
print(corr)

"""1. Difference between Series and DataFrame

Series → One-dimensional labeled data (like a single column).

DataFrame → Two-dimensional table made of multiple Series (rows + columns).

2. Difference between loc and iloc

loc → Label-based indexing (uses column names or row labels).

iloc → Position-based indexing (uses row/column numbers).

3. Purpose of groupby

To group data based on a column and then apply functions like sum, mean,
count, max, etc.

4. How to handle missing values

fillna() → Replace missing values.


dropna() → Remove rows/columns with missing values.

isnull() → Detect missing values.

5. Difference between merge, join, concat

merge → Combine DataFrames based on common columns (SQL-style).

join → Join using index or key column.

concat → Stack DataFrames vertically or horizontally.

6. What is vectorization & why faster?

Vectorization means performing operations on entire arrays/columns at once.


It’s faster because it uses optimized C-level operations instead of slow
Python loops.

7. How to detect and remove duplicates

Detect:

[Link]()

Remove:

df.drop_duplicates()

8. Use of apply() function

It applies a custom function to each row or column in a DataFrame.

9. What is a correlation matrix?

A table showing the relationship (correlation) between numeric columns.


Useful for understanding which variables are strongly related.

10. Change data type of a column


df['col'] = df['col'].astype('int')"""

You might also like