Skip to content

Commit 99f6a6a

Browse files
committed
add solution to exercise
1 parent 0656bde commit 99f6a6a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

content/pandas.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,37 @@ Exercises 3
437437
sns.catplot(x="bornCountry", col="category", data=subset_physchem, kind="count");
438438

439439

440+
.. solution::
441+
442+
We use the :meth:`describe` method:
443+
444+
::
445+
446+
nobel.bornCountryCode.describe()
447+
# count 956
448+
# unique 81
449+
# top US
450+
# freq 287
451+
452+
We see that the US has received the largest number of Nobel prizes,
453+
and 81 countries are represented.
454+
455+
To calculate the age at which laureates receive their prize, we need
456+
to ensure that the "year" and "born" columns are in datetime format::
457+
458+
nobel["born"] = pd.to_datetime(nobel["born"], errors ='coerce')
459+
nobel["year"] = pd.to_datetime(nobel["year"], format="%Y")
460+
461+
Then we add a column with the age at which Nobel prize was received
462+
and plot a histogram::
463+
464+
nobel["age_nobel"] = round((nobel["year"] - nobel["born"]).dt.days / 365, 1)
465+
nobel.hist(column="age_nobel", bins=25, figsize=(8,10), rwidth=0.9)
466+
467+
We can print names of all laureates from a given country, e.g.::
468+
469+
nobel[nobel["country"] == "Sweden"].loc[:, "firstname":"surname"]
470+
440471
Beyond the basics
441472
-----------------
442473

0 commit comments

Comments
 (0)