Skip to content

Party Time Problem


Whenever your friends John and Judy visit you together, y’all have a party 🥳. Given a DataFrame with 10 rows representing the next 10 days of your schedule and whether John and Judy are scheduled to make an appearance, insert a new column called days_til_party that indicates how many days until the next party.

import numpy as np
import pandas as pd

generator = np.random.default_rng(123)
df = pd.DataFrame({
    'john': generator.choice([True, False], size=10, replace=True),
    'judy': generator.choice([True, False], size=10, replace=True)
})
print(df)
#     john   judy
# 0   True   True
# 1  False  False
# 2  False   True
# 3   True  False
# 4  False   True
# 5   True   True
# 6   True  False
# 7   True  False
# 8   True  False
# 9   True  False

days_til_party should be 0 on days when a party occurs, 1 on days when a party doesn’t occur but will occur the next day, etc.


Try with Google Colab See the solution