1) write a python program to create and display a pandas data frame
import pandas as pd
2)
# Create a dictionary
data = {
"Name": ["Alice", "Bob", "Charlie"],
"Age": [25, 30, 35],
"City": ["New York", "London", "Paris"]
}
# Create a DataFrame
df = [Link](data)
# Display the DataFrame
print(df)
Output:
Name Age City
0 Alice 25 New York
1 Bob 30 London
2 Charlie 35 Paris
3) write a python program to read data from CSV file using pandas
import pandas as pd
# Read data from a CSV file
df = pd.read_csv("[Link]")
# Display the DataFrame
print(df)
If [Link] contains:
Name,Age,City
Alice,25,New York
Bob,30,London
Charlie,35,Paris
Output
Name Age City
0 Alice 25 New York
1 Bob 30 London
2 Charlie 35 Paris
Using Microsoft Excel
1. Open Microsoft Excel.
2. Enter the data in rows and columns:
Name Age City
Alice 25 New York
Bob 30 London
Charlie 35 Paris
Click File → Save As.
Choose CSV (Comma delimited) (*.csv) as the file type.
Save the file as [Link].
Method 2: Using Python
You can also create a CSV file with Python:
import pandas as pd
# Create a dictionary
data = {
"Name": ["Alice", "Bob", "Charlie"],
"Age": [25, 30, 35],
"City": ["New York", "London", "Paris"]
}
# Create a DataFrame
df = [Link](data)
# Save the DataFrame to a CSV file
df.to_csv("[Link]", index=False)
print("CSV file created successfully.")
4) write a python program to read data from JSON file and convert it into data frame
import pandas as pd
# Read data from a JSON file
df = pd.read_json("[Link]")
# Display the DataFrame
print(df)
[
{
"Name": "Alice",
"Age": 25,
"City": "New York"
},
{
"Name": "Bob",
"Age": 30,
"City": "London"
},
{
"Name": "Charlie",
"Age": 35,
"City": "Paris"
}
]
Name Age City
0 Alice 25 New York
1 Bob 30 London
2 Charlie 35 Paris
You can create a JSON file in different ways.
Method 1: Using a Text Editor
1. Open Notepad or any text editor.
2. Write data in JSON format:
{
"students": [
{
"Name": "Alice",
"Age": 25,
"City": "New York"
},
{
"Name": "Bob",
"Age": 30,
"City": "London"
},
{
"Name": "Charlie",
"Age": 35,
"City": "Paris"
}
]
}
3. Save the file with the extension [Link].
Method 2: Creating a JSON File Using Python
import json
# Create a dictionary
data = {
"students": [
{
"Name": "Alice",
"Age": 25,
"City": "New York"
},
{
"Name": "Bob",
"Age": 30,
"City": "London"
},
{
"Name": "Charlie",
"Age": 35,
"City": "Paris"
}
]
}
# Write data to a JSON file
with open("[Link]", "w") as file:
[Link](data, file, indent=4)
print("JSON file created successfully.")
Generated [Link] file:
{
"students": [
{
"Name": "Alice",
"Age": 25,
"City": "New York"
},
{
"Name": "Bob",
"Age": 30,
"City": "London"
},
{
"Name": "Charlie",
"Age": 35,
"City": "Paris"
}
]
}
5) write a program to identify missing values in dataset
import pandas as pd
# Load the dataset
df = pd.read_csv("[Link]")
# Identify missing values
missing_values = [Link]()
# Display missing values
print("Missing Values in Dataset:")
print(missing_values)
# Count missing values in each column
missing_count = [Link]().sum()
print("\nCount of Missing Values in Each Column:")
print(missing_count)
Example [Link]
Name,Age,City,Marks
Alice,25,New York,85
Bob,,London,90
Charlie,35,,75
David,40,Paris,
Output
Missing Values in Dataset:
Name Age City Marks
0 False False False False
1 False True False False
2 False False True False
3 False False False True
Count of Missing Values in Each Column:
Name 0
Age 1
City 1
Marks 1
dtype: int64
6) write a program to identify missing values using mean median or mode
import pandas as pd
# Create original dataset
data = {
"Name": ["Alice", "Bob", "Charlie", "David"],
"Age": [25, None, 35, 40],
"City": ["New York", "London", None, "Paris"],
"Marks": [85, 90, 75, None]
}
# Convert dictionary to DataFrame
df = [Link](data)
# Save dataset as CSV file
df.to_csv("[Link]", index=False)
print("Original dataset created successfully.")
# Display dataset
print(df)
Output:
Name Age City Marks
0 Alice 25.0 New York 85.0
1 Bob NaN London 90.0
2 Charlie 35.0 NaN 75.0
3 David 40.0 Paris NaN
import pandas as pd
# Read dataset from CSV file
df = pd.read_csv("[Link]")
# Display original dataset
print("Original Dataset:")
print(df)
# Identify missing values
print("\nMissing Values:")
print([Link]().sum())
# Fill missing numerical values using mean
df["Age"] = df["Age"].fillna(df["Age"].mean())
# Fill missing numerical values using median
df["Marks"] = df["Marks"].fillna(df["Marks"].median())
# Fill missing categorical values using mode
df["City"] = df["City"].fillna(df["City"].mode()[0])
# Display dataset after filling missing values
print("\nDataset After Filling Missing Values:")
print(df)
Name,Age,City,Marks
Alice,25,New York,85
Bob,,London,90
Charlie,35,,75
David,40,Paris,
Output:
Original Dataset:
Name Age City Marks
0 Alice 25.0 New York 85.0
1 Bob NaN London 90.0
2 Charlie 35.0 NaN 75.0
3 David 40.0 Paris NaN
Missing Values:
Name 0
Age 1
City 1
Marks 1
Dataset After Filling Missing Values:
Name Age City Marks
0 Alice 25.0 New York 85.0
1 Bob 33.3 London 90.0
2 Charlie 35.0 New York 75.0
3 David 40.0 Paris 85.0