I have a total of about 9000 rows and 26 different units in the df. An example of the df is below:
Unit | Total_time |
---|---|
E271 | 0 days 00:05:32 |
I want to create a column with the amount of times each unit appears in the df and a separate one that counts the amount of times each unit’s time is under 6 min. The new df should look like this:
Unit | Total_dispatches | Amount_time_under | Perc_success_rate |
---|---|---|---|
E271 | 1154 | 883 | 76.5% |
Any help would be appreciated!
Advertisement
Answer
You can do it something like this:
JavaScript
x
18
18
1
data = [
2
{"Unit": "E1", "Total_time":pd.to_timedelta('0 days 00:05:01.00003')},
3
{"Unit": "E1", "Total_time":pd.to_timedelta('0 days 00:07:01.00003')},
4
{"Unit": "E1", "Total_time":pd.to_timedelta('0 days 00:05:01.00003')}
5
]
6
7
df = pd.DataFrame(data)
8
9
10
df["Total_dispatches"] = df.groupby("Unit").transform("count")
11
12
time_limit = pd.to_timedelta('0 days 00:06:00.00000')
13
df["under_6_min"] = (df["Total_time"]<time_limit).astype(int)
14
15
df["Amount_time_under"] = df.groupby("Unit")["under_6_min"].transform("sum")
16
17
df["Perc_success_rate"] = (df["Amount_time_under"]/df["Total_dispatches"])*100
18