Pandas Series Exercise with Solutions

Create a Pandas Series of integers and find the mean, median, and mode of the series.

import pandas as pd

s = pd.Series([3, 2, 4, 1, 5, 2, 7, 8, 1, 4])
mean = s.mean()
median = s.median()
mode = s.mode()

print("Mean:", mean)
print("Median:", median)
print("Mode:", mode[0])

Output -
Mean: 3.7
Median: 3.5
Mode: 2


Create a Pandas Series of strings and count the number of occurrences of a specific string.
import pandas as pd

s = pd.Series(["apple", "banana", "orange", "banana", "apple"])
count = s.value_counts()["apple"]

print("Count of apples:", count)

Output - 

Count of apples: 2


Create a Pandas Series of dates and find the earliest and latest date in the series.

import pandas as pd

s = pd.Series(['2022-01-01', '2021-12-31', '2022-03-15', '2022-02-01'])
earliest = pd.to_datetime(s).min()
latest = pd.to_datetime(s).max()

print("Earliest date:", earliest)
print("Latest date:", latest)

Earliest date: 2021-12-31 00:00:00
Latest date: 2022-03-15 00:00:00


Create a Pandas Series of boolean values and count the number of True values.

import pandas as pd

s = pd.Series([True, False, True, True, False])
count = s.value_counts()[True]

print("Count of True values:", count)

Count of True values: 3


Create a Pandas Series of floating-point numbers and round the values to the nearest integer.

import pandas as pd

s = pd.Series([3.14, 1.5, 2.7, 4.3, 5.8])
rounded = s.round()

print("Rounded values:", rounded)

Rounded values:
0 3.0
1 2.0
2 3.0
3 4.0
4 6.0
dtype: float64

Create a Pandas Series of integers and replace all even values with 0.

import pandas as pd

s = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9])
s[s % 2 == 0] = 0

print("Series with even values replaced:", s)

Series with even values replaced:
0 1
1 0
2 3
3 0
4 5
5 0
6 7
7 0
8 9
dtype: int64


Create a Pandas Series of strings and extract a substring from each element using regular expressions.

import pandas as pd
import re

s = pd.Series(["apple123", "banana456", "orange789"])
pattern = "\d+"
substrings = s.str.extract(pattern)

print("Extracted substrings:", substrings)

Extracted substrings:
     0
0 123
1 456
2 789


Create a Pandas Series of dates and add a certain number of days to each date.

import pandas as pd

s = pd.Series(['2022-01-01', '2021-12-31', '2022-03-15', '2022-02-01'])
s = pd.to_datetime(s) + pd.Timedelta(days=10)

print("New dates:", s)

New dates:
0 2022-01-11
1 2022-01-10
2 2022-03-25
3 2022-02-11
dtype: datetime64[ns]

Create a Pandas Series of floating-point numbers and find the cumulative sum of the values.

import pandas as pd

s = pd.Series([1.2, 2.3, 3.4, 4.5, 5.6])
cumulative_sum = s.cumsum()

print("Cumulative sum:", cumulative_sum)

Cumulative sum:
0 1.2
1 3.5
2 6.9
3 11.4
4 17.0
dtype: float64

Create a Pandas Series of integers and find the second largest value in the series.

import pandas as pd

s = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9])
second_largest = s.nlargest(2).min()

print("Second largest value:", second_largest)

Second largest value: 8

Post a Comment

0 Comments