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

Python Data Analysis with NumPy & Pandas

The document contains a series of Python programs demonstrating various data manipulation and analysis techniques using NumPy and Pandas. It covers topics such as creating arrays, calculating statistics, reading and exporting CSV files, handling missing values, and performing linear regression. Additionally, it includes examples of grouping data and preprocessing datasets.

Uploaded by

trippin
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)
9 views4 pages

Python Data Analysis with NumPy & Pandas

The document contains a series of Python programs demonstrating various data manipulation and analysis techniques using NumPy and Pandas. It covers topics such as creating arrays, calculating statistics, reading and exporting CSV files, handling missing values, and performing linear regression. Additionally, it includes examples of grouping data and preprocessing datasets.

Uploaded by

trippin
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 Programs

Q1: NumPy – 1D Array and Statistics


import numpy as np

arr = [Link](10, 51)


print("Array:", arr)

print("Mean:", [Link](arr))
print("Median:", [Link](arr))
print("Standard Deviation:", [Link](arr))

Q2: Pandas – DataFrame from Dictionary


import pandas as pd

data = {'Name': ['Aman', 'Riya', 'Sohan', 'Meena'],


'Marks': [85, 90, 78, 92]}

df = [Link](data)
print([Link](3))

Q3: Read CSV File


import pandas as pd

df = pd.read_csv("[Link]")
print("Column Names:", [Link])

Q4: Export DataFrame to CSV


import pandas as pd

data = {'Name': ['Aman', 'Riya'],


'Marks': [85, 90]}

df = [Link](data)
df.to_csv("[Link]", index=False)
print("File exported successfully!")
Q5:Add New Column to DataFrame
import pandas as pd

data = {'Name': ['Aman', 'Riya'],


'Marks': [85, 90]}

df = [Link](data)
df['Grade'] = ['A', 'A+']
print(df)

Q6: Replace Missing Values with Mean


import pandas as pd
import numpy as np

data = {'Marks': [85, [Link], 78, [Link], 92]}


df = [Link](data)

df['Marks'] = df['Marks'].fillna(df['Marks'].mean())
print(df)

Q7: Drop Rows with Missing Values


import pandas as pd
import numpy as np

data = {'Name': ['Aman', 'Riya', 'Sohan'],


'Marks': [85, [Link], 90]}

df = [Link](data)
df = [Link]()
print(df)

Q8: Group Sales Data


import pandas as pd

data = {'Product': ['Pen', 'Pen', 'Book', 'Book'],


'Sales': [100, 200, 300, 150]}

df = [Link](data)
result = [Link]('Product')['Sales'].sum()
print(result)
Q9: Add New Row
import pandas as pd

data = {'Name': ['Aman', 'Riya'],


'Marks': [85, 90]}

df = [Link](data)
new_row = {'Name': 'Sohan', 'Marks': 88}
df = [Link](new_row, ignore_index=True)
print(df)

Q10: Display Shape, Index, and Columns


import pandas as pd

data = {'Name': ['Aman', 'Riya'],


'Marks': [85, 90]}

df = [Link](data)
print("Shape:", [Link])
print("Index:", [Link])
print("Columns:", [Link])

Q11: Linear Regression with sklearn


import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression

X_train = [[1], [2], [3], [4], [5]]


y_train = [2, 4, 6, 8, 10]
X_test = [[6], [7]]

model = LinearRegression()
[Link](X_train, y_train)
predictions = [Link](X_test)

print("Predictions:", predictions)
Q12: Analyze Marketing Dataset
import pandas as pd

data = {'Campaign Type': ['A', 'A', 'B', 'B'],


'Sales': [200, 250, 300, 400],
'Engagement': [100, 120, 150, 170]}

df = [Link](data)
result = [Link]('Campaign Type')[['Sales', 'Engagement']].mean()
print(result)

Q13: Clean and Preprocess Employee Data


import pandas as pd

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

# Drop irrelevant columns


df = [Link](['Address', 'Phone'], axis=1)

# Fill missing values


df = [Link]([Link](numeric_only=True))

print([Link]())

You might also like