Page 21 - Informatics_Practices_Fliipbook_Class12
P. 21
>>> nationalSports = pd.Series(sportsDict)
>>> print(nationalSports)
output:
Bhutan Archery
Scotland Golf
Japan Sumo
India Hockey
dtype: object
Next, we create another series object for the countries that have a passion for cricket.
>>> countries = ['India', 'Australia', 'Barbados', 'Pakistan', 'England']
>>> cricketLovers = pd.Series(['Cricket']*5, index=countries)
>>> print(cricketLovers)
output:
India Cricket
Australia Cricket
Barbados Cricket
Pakistan Cricket
England Cricket
dtype: object
Finally, we combine the two series to form the series allSports. To achieve this, we use the append() method of
the Series object, as shown below:
>>> allSports = nationalSports.append(cricketLovers)
>>> print(allSports)
Bhutan Archery
Scotland Golf
Japan Sumo
India Hockey
India Cricket
Australia Cricket
Barbados Cricket
Pakistan Cricket
England Cricket
dtype: object
Note that labels associated with values in a Pandas Series do not need to be unique. In the series, allSports, the
index 'India' appears twice. Next, suppose, we want to list the countries where a sport is popular. A straightforward
way to do this would be to construct a series by swapping the role of the index (allSports.index) and the values
(allSports.values), as demonstrated below:
>>> allSportsInverted = pd.Series(allSports.index, index = allSports.values)
>>> print(allSportsInverted)
output:
Archery Bhutan
Golf Scotland
Sumo Japan
Hockey India
Cricket India
Cricket Australia
Cricket Barbados
Cricket Pakistan
Cricket England
dtype: object
The pandas version upto 2.xxxx stores series object as the numpy array at the backend, which are both space and
computationally efficient. In the latest versions of pandas let us use PandasArrow as the backend which is mainly
useful when dealing with time series data.
Data Handling using Pandas 7

