PYTHON
1) Write a Python program to create the Pandas DataFrame displayed below using a list
of dictionaries.
Course Duration
0 Data Science 12
1 Artificial Intelligence 18
2 Web Development 6
import pandas as pd
d1 = {'Course': 'Data Science', 'Duration': 12}
d2 = {'Course': 'Artificial Intelligence', 'Duration': 18}
d3 = {'Course': 'Web Development', 'Duration': 6}
data = [d1, d2, d3]
df = pd.DataFrame(data)
print(df)
2) Write a Python program to create the following DataFrame using a list of dictionaries.
City Population Area
Delhi 19000000 1484
Mumbai 20000000 603
Kolkata 15000000 205
Chennai 12000000 426
import pandas as pd
data = [
{"City": "Delhi", "Population": 19000000, "Area": 1484},
{"City": "Mumbai", "Population": 20000000, "Area": 603},
{"City": "Kolkata", "Population": 15000000, "Area": 205},
{"City": "Chennai", "Population": 12000000, "Area": 426}]
df = pd.DataFrame(data)
print(df)
3) Write a Python program to create a Pandas Series as shown below using a ndarray,
where the subject names are the indices and the corresponding marks are the values in
the series.
Mathematics 85
Science 90
English 78
History 88
import pandas as pd
import numpy as np
marks = np.array([85, 90, 78, 88])
series = pd.Series(marks, index=['Mathematics', 'Science', 'English', 'History'])
print(series)
4) Write a Python Program to create a Pandas Series as shown below using a dictionary.
Note that the left column indicates the indices and the right column displays the data.
India New Delhi
France Paris
Japan Tokyo
Australia Canberra
import pandas as pd
capitals = { "India": "New Delhi", "France": "Paris", "Japan": "Tokyo", "Canada":
"Ottawa"}
series = pd.Series(capitals)
print(series)
5) Consider the DataFrame df shown below.
Name Department Salary
0 Rohan Sharme IT 75000
1 Meera Kapoor HR 68000
2 Aarav Singh Finance 85000
3 Nisha Singh Marketing 72000
4 Aditya Verma IT 80000
Write Python statements for the following tasks:
a) Print the last three rows of the DataFrame df.
b) Add a new column named "Experience" with values [5, 8, 10, 6, 7].
c) Delete the column "Salary" from the DataFrame.
d) Rename the column "Department" to "Dept".
e) Display only the "Name" and "Salary" columns from the DataFrame.
Ans:
a) print(df.tail(3))
b) df['Experience'] = [5, 8, 10, 6, 7]
c) df.drop(columns=['Salary'], inplace=True)
d) df.rename(columns={'Department': 'Dept'}, inplace=True)
e) print(df[["Name", "Salary"]])