Puzzle 23: Chain of Commands

Let's solve a pandas DataFrame and Timedelta puzzle.

We'll cover the following

Guess the output

Try to guess the output of the code below:

Press + to interact
import pandas as pd
df = pd.DataFrame([
['133.43.96.45', pd.Timedelta('3s')],
['133.68.18.180', pd.Timedelta('2s')],
['133.43.96.45', pd.NaT],
['133.43.96.45', pd.Timedelta('4s')],
['133.43.96.45', pd.Timedelta('2s')],
], columns=['ip', 'duration'])
by_ip = (
df['duration']
.fillna(pd.Timedelta(seconds=1))
.groupby(df['ip'])
.sum()
)
print(by_ip)

Quiz

Q

What is the output of the code above?

A)
ip
133.43.96.45 00:00:09
133.68.18.180 00:00:02
Name: duration, dtype: timedelta64[ns]
B)
ip
133.43.96.45    00:00:10
133.68.18.180   00:00:02
Name: duration, dtype: timedelta64[ns]
C)
ip
133.43.96.45 00:00:10
Name: duration, dtype: timedelta64[ns]
D)
ip
133.43.96.45 00:00:03
133.68.18.180 00:00:02
Name: duration, dtype: timedelta64[ns]

Get hands-on with 1300+ tech skills courses.