1/19/25, 8:54 PM
2.Quiz Test_Pandas
National Institute of Technology
Rourkela, Odisha, India, 769008
Department of Computer Science Engineering
Lab-2: AI & ML Lab
(Pandas Quiz Test)
1. Write a program to initialize a dictionary (of your choice) and create a pandas
DataFrame from it. Print the dataframe?
In [68]: import pandas as pd
data = {
'Name of students': ['Ankit', 'Rahul', 'rohit'],
'Branch': ['CSE','EIE','ECE'],
'Their ages': [19, 20, 21],
'Cities': ['rourkela', 'cuttack', 'sambalpur']
}
print(pd.DataFrame(data))
Name of students Branch
0
Ankit
CSE
1
Rahul
EIE
2
rohit
ECE
Their ages
19
20
21
Cities
rourkela
cuttack
sambalpur
2. Write a program to read the CSV file "summer.csv" into a pandas DataFrame "df",
display the first 5 rows, and print the DataFrame's shape
In [5]: df = pd.read_csv("summer.csv")
print(df.head())
print(df.shape)
file:///C:/Download Items/2.Quiz Test_Pandas.html
1/13
1/19/25, 8:54 PM
2.Quiz Test_Pandas
0
1
2
3
4
Year
1896
1896
1896
1896
1896
City
Athens
Athens
Athens
Athens
Athens
Sport Discipline
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Event
0
100M Freestyle
1
100M Freestyle
2 100M Freestyle For Sailors
3 100M Freestyle For Sailors
4 100M Freestyle For Sailors
(31165, 9)
Athlete Country Gender
HAJOS, Alfred
HUN
Men
HERSCHMANN, Otto
AUT
Men
DRIVAS, Dimitrios
GRE
Men
MALOKINIS, Ioannis
GRE
Men
CHASAPIS, Spiridon
GRE
Men
\
Medal
Gold
Silver
Bronze
Gold
Silver
3. Write a program to display the last 10 rows of the DataFrame?
In [9]: print(df.head(10))
0
1
2
3
4
5
6
7
8
9
Year
1896
1896
1896
1896
1896
1896
1896
1896
1896
1896
City
Athens
Athens
Athens
Athens
Athens
Athens
Athens
Athens
Athens
Athens
Sport Discipline
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
0
1
2
3
4
5
6
7
8
9
Event
100M Freestyle
100M Freestyle
100M Freestyle For Sailors
100M Freestyle For Sailors
100M Freestyle For Sailors
1200M Freestyle
1200M Freestyle
1200M Freestyle
400M Freestyle
400M Freestyle
Athlete Country Gender
HAJOS, Alfred
HUN
Men
HERSCHMANN, Otto
AUT
Men
DRIVAS, Dimitrios
GRE
Men
MALOKINIS, Ioannis
GRE
Men
CHASAPIS, Spiridon
GRE
Men
CHOROPHAS, Efstathios
GRE
Men
HAJOS, Alfred
HUN
Men
ANDREOU, Joannis
GRE
Men
CHOROPHAS, Efstathios
GRE
Men
NEUMANN, Paul
AUT
Men
\
Medal
Gold
Silver
Bronze
Gold
Silver
Bronze
Gold
Silver
Bronze
Gold
4. Write a program to get a summary of the DataFrame (such as the mean, count, etc.)
and print the data types of all columns?
In [11]: print(df.describe())
file:///C:/Download Items/2.Quiz Test_Pandas.html
2/13
1/19/25, 8:54 PM
2.Quiz Test_Pandas
count
mean
std
min
25%
50%
75%
max
Year
31165.000000
1970.482785
33.158416
1896.000000
1948.000000
1980.000000
2000.000000
2012.000000
5. Write a program to select a single column "Gender" from the DataFrame?
In [13]: print(df['Gender'])
0
1
2
3
4
Men
Men
Men
Men
Men
...
31160
Men
31161
Men
31162
Men
31163
Men
31164
Men
Name: Gender, Length: 31165, dtype: object
6. Write a program to select multiple columns "Year" and "City" from the DataFrame?
In [19]: print(df[['Year','City']])
0
1
2
3
4
...
31160
31161
31162
31163
31164
Year
1896
1896
1896
1896
1896
...
2012
2012
2012
2012
2012
City
Athens
Athens
Athens
Athens
Athens
...
London
London
London
London
London
[31165 rows x 2 columns]
7. Write a program to filter rows in a DataFrame based on the condition that Gender =
Men ?
In [23]: print(df[df['Gender'] == 'Men'])
file:///C:/Download Items/2.Quiz Test_Pandas.html
3/13
1/19/25, 8:54 PM
2.Quiz Test_Pandas
0
1
2
3
4
...
31160
31161
31162
31163
31164
Year
1896
1896
1896
1896
1896
...
2012
2012
2012
2012
2012
City
Athens
Athens
Athens
Athens
Athens
...
London
London
London
London
London
Sport
Aquatics
Aquatics
Aquatics
Aquatics
Aquatics
...
Wrestling
Wrestling
Wrestling
Wrestling
Wrestling
Discipline
Swimming
Swimming
Swimming
Swimming
Swimming
...
Wrestling Freestyle
Wrestling Freestyle
Wrestling Freestyle
Wrestling Freestyle
Wrestling Freestyle
0
1
2
3
4
...
31160
31161
31162
31163
31164
Athlete Country Gender
HAJOS, Alfred
HUN
Men
HERSCHMANN, Otto
AUT
Men
DRIVAS, Dimitrios
GRE
Men
MALOKINIS, Ioannis
GRE
Men
CHASAPIS, Spiridon
GRE
Men
...
...
...
JANIKOWSKI, Damian
POL
Men
REZAEI, Ghasem Gholamreza
IRI
Men
TOTROV, Rustam
RUS
Men
ALEKSANYAN, Artur
ARM
Men
LIDBERG, Jimmy
SWE
Men
0
1
2
3
4
...
31160
31161
31162
31163
31164
Medal
Gold
Silver
Bronze
Gold
Silver
...
Bronze
Gold
Silver
Bronze
Bronze
\
Event
100M Freestyle
100M Freestyle
100M Freestyle For Sailors
100M Freestyle For Sailors
100M Freestyle For Sailors
...
Wg 84 KG
Wg 96 KG
Wg 96 KG
Wg 96 KG
Wg 96 KG
\
[22746 rows x 9 columns]
8. Write a program to select and print the first 5 rows and columns 2 to 6 (inclusive) of
the DataFrame. Also, print the type of the resulting object?
In [25]: print(df.iloc[:5, 2:7])
0
1
2
3
4
Sport Discipline
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
file:///C:/Download Items/2.Quiz Test_Pandas.html
Athlete Country Gender
HAJOS, Alfred
HUN
Men
HERSCHMANN, Otto
AUT
Men
DRIVAS, Dimitrios
GRE
Men
MALOKINIS, Ioannis
GRE
Men
CHASAPIS, Spiridon
GRE
Men
4/13
1/19/25, 8:54 PM
2.Quiz Test_Pandas
9. Write a program to delete a column "City" from the DataFrame and print the new
dataframe??
In [27]: print(df.drop('City', axis=1))
0
1
2
3
4
...
31160
31161
31162
31163
31164
Year
1896
1896
1896
1896
1896
...
2012
2012
2012
2012
2012
Sport
Aquatics
Aquatics
Aquatics
Aquatics
Aquatics
...
Wrestling
Wrestling
Wrestling
Wrestling
Wrestling
Country Gender
0
HUN
Men
1
AUT
Men
2
GRE
Men
3
GRE
Men
4
GRE
Men
...
...
...
31160
POL
Men
31161
IRI
Men
31162
RUS
Men
31163
ARM
Men
31164
SWE
Men
Discipline
Swimming
Swimming
Swimming
Swimming
Swimming
...
Wrestling Freestyle
Wrestling Freestyle
Wrestling Freestyle
Wrestling Freestyle
Wrestling Freestyle
Athlete
HAJOS, Alfred
HERSCHMANN, Otto
DRIVAS, Dimitrios
MALOKINIS, Ioannis
CHASAPIS, Spiridon
...
JANIKOWSKI, Damian
REZAEI, Ghasem Gholamreza
TOTROV, Rustam
ALEKSANYAN, Artur
LIDBERG, Jimmy
Event
100M Freestyle
100M Freestyle
100M Freestyle For Sailors
100M Freestyle For Sailors
100M Freestyle For Sailors
...
Wg 84 KG
Wg 96 KG
Wg 96 KG
Wg 96 KG
Wg 96 KG
\
Medal
Gold
Silver
Bronze
Gold
Silver
...
Bronze
Gold
Silver
Bronze
Bronze
[31165 rows x 8 columns]
10. Write a program to rename a column "Athlete" to "Participants" in the DataFrame?
In [29]: print(df.rename(columns={'Athlete': 'Participants'}))
file:///C:/Download Items/2.Quiz Test_Pandas.html
5/13
1/19/25, 8:54 PM
2.Quiz Test_Pandas
0
1
2
3
4
...
31160
31161
31162
31163
31164
Year
1896
1896
1896
1896
1896
...
2012
2012
2012
2012
2012
City
Athens
Athens
Athens
Athens
Athens
...
London
London
London
London
London
Sport
Aquatics
Aquatics
Aquatics
Aquatics
Aquatics
...
Wrestling
Wrestling
Wrestling
Wrestling
Wrestling
Discipline
Swimming
Swimming
Swimming
Swimming
Swimming
...
Wrestling Freestyle
Wrestling Freestyle
Wrestling Freestyle
Wrestling Freestyle
Wrestling Freestyle
0
1
2
3
4
...
31160
31161
31162
31163
31164
Participants Country Gender
HAJOS, Alfred
HUN
Men
HERSCHMANN, Otto
AUT
Men
DRIVAS, Dimitrios
GRE
Men
MALOKINIS, Ioannis
GRE
Men
CHASAPIS, Spiridon
GRE
Men
...
...
...
JANIKOWSKI, Damian
POL
Men
REZAEI, Ghasem Gholamreza
IRI
Men
TOTROV, Rustam
RUS
Men
ALEKSANYAN, Artur
ARM
Men
LIDBERG, Jimmy
SWE
Men
0
1
2
3
4
...
31160
31161
31162
31163
31164
Medal
Gold
Silver
Bronze
Gold
Silver
...
Bronze
Gold
Silver
Bronze
Bronze
\
Event
100M Freestyle
100M Freestyle
100M Freestyle For Sailors
100M Freestyle For Sailors
100M Freestyle For Sailors
...
Wg 84 KG
Wg 96 KG
Wg 96 KG
Wg 96 KG
Wg 96 KG
\
[31165 rows x 9 columns]
12. Write a program to display the first 10 rows of the df DataFrame and check for any
missing values in each column.?
In [44]: print(df.head(10))
print(df.isnull().sum())
file:///C:/Download Items/2.Quiz Test_Pandas.html
6/13
1/19/25, 8:54 PM
2.Quiz Test_Pandas
0
1
2
3
4
5
6
7
8
9
Year
1896
1896
1896
1896
1896
1896
1896
1896
1896
1896
City
Athens
Athens
Athens
Athens
Athens
Athens
Athens
Athens
Athens
Athens
Sport Discipline
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Aquatics
Swimming
Event
0
100M Freestyle
1
100M Freestyle
2 100M Freestyle For Sailors
3 100M Freestyle For Sailors
4 100M Freestyle For Sailors
5
1200M Freestyle
6
1200M Freestyle
7
1200M Freestyle
8
400M Freestyle
9
400M Freestyle
Year
0
City
0
Sport
0
Discipline
0
Athlete
0
Country
4
Gender
0
Event
0
Medal
0
dtype: int64
Athlete Country Gender
HAJOS, Alfred
HUN
Men
HERSCHMANN, Otto
AUT
Men
DRIVAS, Dimitrios
GRE
Men
MALOKINIS, Ioannis
GRE
Men
CHASAPIS, Spiridon
GRE
Men
CHOROPHAS, Efstathios
GRE
Men
HAJOS, Alfred
HUN
Men
ANDREOU, Joannis
GRE
Men
CHOROPHAS, Efstathios
GRE
Men
NEUMANN, Paul
AUT
Men
\
Medal
Gold
Silver
Bronze
Gold
Silver
Bronze
Gold
Silver
Bronze
Gold
13. Write a program to select and print the "Country" and "Medal" columns from the
df DataFrame, and then print the number of unique values in each of these columns.?
In [46]: print(df[["Country", "Medal"]])
print((df[["Country", "Medal"]])["Country"].nunique())
print((df[["Country", "Medal"]])["Medal"].nunique())
file:///C:/Download Items/2.Quiz Test_Pandas.html
7/13
1/19/25, 8:54 PM
2.Quiz Test_Pandas
0
1
2
3
4
...
31160
31161
31162
31163
31164
Country
HUN
AUT
GRE
GRE
GRE
...
POL
IRI
RUS
ARM
SWE
Medal
Gold
Silver
Bronze
Gold
Silver
...
Bronze
Gold
Silver
Bronze
Bronze
[31165 rows x 2 columns]
147
3
14. Write a program to group the df DataFrame by the "Country" column and
calculate the total number of medals for each country. Print the result.?
In [48]: print(df.groupby("Country")["Medal"].count())
Country
AFG
2
AHO
1
ALG
15
ANZ
29
ARG
259
...
VIE
2
YUG
435
ZAM
2
ZIM
23
ZZX
48
Name: Medal, Length: 147, dtype: int64
15. Write a program to sort the df DataFrame by the "Year" column in descending
order and print the first 5 rows of the sorted DataFrame.?
In [50]: print(df.sort_values(by="Year", ascending=False).head)
file:///C:/Download Items/2.Quiz Test_Pandas.html
8/13
1/19/25, 8:54 PM
2.Quiz Test_Pandas
<bound method NDFrame.head of
Athlete \
31164 2012 London
Wrestling
29885 2012 London
Boxing
29857 2012 London Basketball
29858 2012 London Basketball
29859 2012 London Basketball
...
...
...
...
101
1896 Athens Gymnastics
100
1896 Athens Gymnastics
99
1896 Athens Gymnastics
98
1896 Athens Gymnastics
0
1896 Athens
Aquatics
31164
29885
29857
29858
29859
...
101
100
99
98
0
Country Gender
SWE
Men
IRL
Men
AUS Women
AUS Women
AUS Women
...
...
GER
Men
GER
Men
GER
Men
GRE
Men
HUN
Men
Year
City
Sport
Wrestling Freestyle
Boxing
Basketball
Basketball
Basketball
...
Artistic G.
Artistic G.
Artistic G.
Artistic G.
Swimming
Event
Wg 96 KG
56KG
Basketball
Basketball
Basketball
...
Team, Parallel Bars
Team, Parallel Bars
Team, Parallel Bars
Team, Parallel Bars
100M Freestyle
Discipline
LIDBERG, Jimmy
NEVIN, John Joe
BISHOP, Abby
CAMBAGE, Elizabeth
HARROWER, Kristi
...
FLATOW, Gustav Felix
FLATOW, Alfred
BÖCKER, Konrad
MITROPOULOS, Ioannis
HAJOS, Alfred
Medal
Bronze
Silver
Bronze
Bronze
Bronze
...
Gold
Gold
Gold
Bronze
Gold
[31165 rows x 9 columns]>
16. Write a program to add a new column "Century" to the df DataFrame. Classify
each event as occurring in the "20th Century" if the "Year" is less than 2000, or the
"21st Century" if the "Year" is 2000 or later. Print the first 5 rows to verify the change.
In [54]: df = pd.read_csv("summer.csv", index_col="Athlete")
df["Century"] = df["Year"].apply(lambda x: "20th Century" if x < 2000 else "21st Ce
print(df.head())
Athlete
HAJOS, Alfred
HERSCHMANN, Otto
DRIVAS, Dimitrios
MALOKINIS, Ioannis
CHASAPIS, Spiridon
Athlete
HAJOS, Alfred
HERSCHMANN, Otto
DRIVAS, Dimitrios
MALOKINIS, Ioannis
CHASAPIS, Spiridon
file:///C:/Download Items/2.Quiz Test_Pandas.html
Year
City
1896
1896
1896
1896
1896
Athens
Athens
Athens
Athens
Athens
Sport Discipline Country Gender
Aquatics
Aquatics
Aquatics
Aquatics
Aquatics
Swimming
Swimming
Swimming
Swimming
Swimming
HUN
AUT
GRE
GRE
GRE
\
Men
Men
Men
Men
Men
Event
Medal
Century
100M Freestyle
100M Freestyle
100M Freestyle For Sailors
100M Freestyle For Sailors
100M Freestyle For Sailors
Gold
Silver
Bronze
Gold
Silver
20th Century
20th Century
20th Century
20th Century
20th Century
9/13
1/19/25, 8:54 PM
2.Quiz Test_Pandas
17. Write a program to filter and print rows from the df DataFrame where the
"Country" is "USA" and the "Medal" is "Gold". Also, count the number of rows that
meet this condition.
In [56]: usa_gold = df.loc[(df["Country"] == "USA") & (df["Medal"] == "Gold")]
print(usa_gold)
print(len(usa_gold))
Athlete
BURKE, Thomas
CURTIS, Thomas
BURKE, Thomas
GARRETT, Robert
CLARK, Ellery
...
WILLIAMS, Serena
MAY, Misty
WALSH JENNINGS, Kerri
BURROUGHS, Jordan Ernest
VARNER, Jacob Stephen
Year
City
Sport
Discipline
1896
1896
1896
1896
1896
...
2012
2012
2012
2012
2012
Athens
Athens
Athens
Athens
Athens
...
London
London
London
London
London
Athletics
Athletics
Athletics
Athletics
Athletics
...
Tennis
Volleyball
Volleyball
Wrestling
Wrestling
Athletics
Athletics
Athletics
Athletics
Athletics
...
Tennis
Beach Volleyball
Beach Volleyball
Wrestling Freestyle
Wrestling Freestyle
Country Gender
Athlete
BURKE, Thomas
CURTIS, Thomas
BURKE, Thomas
GARRETT, Robert
CLARK, Ellery
...
WILLIAMS, Serena
MAY, Misty
WALSH JENNINGS, Kerri
BURROUGHS, Jordan Ernest
VARNER, Jacob Stephen
USA
USA
USA
USA
USA
...
USA
USA
USA
USA
USA
Men
Men
Men
Men
Men
...
Women
Women
Women
Men
Men
Event Medal
100M
110M Hurdles
400M
Discus Throw
High Jump
...
Singles
Beach Volleyball
Beach Volleyball
Wf 74 KG
Wf 96 KG
Gold
Gold
Gold
Gold
Gold
...
Gold
Gold
Gold
Gold
Gold
\
Century
20th Century
20th Century
20th Century
20th Century
20th Century
...
21st Century
21st Century
21st Century
21st Century
21st Century
[2235 rows x 9 columns]
2235
18. Write a program to create a new column "Medal Points" in the df DataFrame.
Assign 3 points for "Gold", 2 points for "Silver", and 1 point for "Bronze". Print the
first 5 rows to verify the changes.
In [58]: def medal_points(medal):
if medal == "Gold":
return 3
elif medal == "Silver":
return 2
elif medal == "Bronze":
return 1
else:
return 0
file:///C:/Download Items/2.Quiz Test_Pandas.html
10/13
1/19/25, 8:54 PM
2.Quiz Test_Pandas
df["Medal Points"] = df["Medal"].apply(medal_points)
print(df.head())
Athlete
HAJOS, Alfred
HERSCHMANN, Otto
DRIVAS, Dimitrios
MALOKINIS, Ioannis
CHASAPIS, Spiridon
Athlete
HAJOS, Alfred
HERSCHMANN, Otto
DRIVAS, Dimitrios
MALOKINIS, Ioannis
CHASAPIS, Spiridon
Year
City
1896
1896
1896
1896
1896
Athens
Athens
Athens
Athens
Athens
Sport Discipline Country Gender
Aquatics
Aquatics
Aquatics
Aquatics
Aquatics
Swimming
Swimming
Swimming
Swimming
Swimming
HUN
AUT
GRE
GRE
GRE
\
Men
Men
Men
Men
Men
Event
Medal
Century
100M Freestyle
100M Freestyle
100M Freestyle For Sailors
100M Freestyle For Sailors
100M Freestyle For Sailors
Gold
Silver
Bronze
Gold
Silver
20th Century
20th Century
20th Century
20th Century
20th Century
\
Medal Points
Athlete
HAJOS, Alfred
HERSCHMANN, Otto
DRIVAS, Dimitrios
MALOKINIS, Ioannis
CHASAPIS, Spiridon
3
2
1
3
2
19. Write a program to filter the df DataFrame to include only rows where the
"Country" is "USA". Then, group the filtered DataFrame by "Year" and calculate the
total number of medals won by the USA each year. Print the results.
In [60]: usa_df = df[df["Country"] == "USA"]
usa_medals_by_year = usa_df.groupby("Year")["Medal"].count()
print(usa_medals_by_year)
file:///C:/Download Items/2.Quiz Test_Pandas.html
11/13
1/19/25, 8:54 PM
2.Quiz Test_Pandas
Year
1896
20
1900
55
1904
394
1908
63
1912
101
1920
193
1924
198
1928
84
1932
181
1936
92
1948
148
1952
130
1956
118
1960
112
1964
150
1968
149
1972
155
1976
155
1984
333
1988
193
1992
224
1996
260
2000
248
2004
264
2008
315
2012
250
Name: Medal, dtype: int64
20. Write a program to identify the athlete who has won the most medals. Display the
athlete's name and the total number of medals they have won.
In [64]: most_successful_athlete = df["Medal"].groupby(df.index).count().idxmax()
total_medals = df["Medal"].groupby(df.index).count().max()
print(f"The most successful athlete is {most_successful_athlete} with {total_medals
The most successful athlete is PHELPS, Michael with 22 medals.
21. Write a program to group the df DataFrame by the "Sport" column and calculate
the number of each type of medal (Gold, Silver, Bronze) for each sport. Print the
results.
In [66]: print(df.groupby(["Sport", "Medal"])["Medal"].count())
file:///C:/Download Items/2.Quiz Test_Pandas.html
12/13
1/19/25, 8:54 PM
2.Quiz Test_Pandas
Sport
Aquatics
Medal
Bronze
Gold
Silver
Bronze
Gold
1365
1421
1384
Archery
90
126
...
Weightlifting Gold
200
Silver
196
Wrestling
Bronze
428
Gold
391
Silver
393
Name: Medal, Length: 124, dtype: int64
Good Luck!
file:///C:/Download Items/2.Quiz Test_Pandas.html
13/13