I have the following dataframe:
index | season | round | number | driverId | position | time |
---|---|---|---|---|---|---|
0 | 1996 | 1 | 1 | villeneuve | 1 | 1:43.702 |
1 | 1996 | 1 | 1 | damon_hill | 2 | 1:44.243 |
2 | 1996 | 1 | 1 | irvine | 3 | 1:44.981 |
with df_laps[['ms']] = 0
I can create a new column ms with all rows containing value = 0.
index | season | round | number | driverId | position | time | ms |
---|---|---|---|---|---|---|---|
0 | 1996 | 1 | 1 | villeneuve | 1 | 1:43.702 | 0 |
1 | 1996 | 1 | 1 | damon_hill | 2 | 1:44.243 | 0 |
2 | 1996 | 1 | 1 | irvine | 3 | 1:44.981 | 0 |
Nevertheless the value of ms must be related to the value of time. So I tried with the following df_laps[['ms']] = df_laps.loc[0, ['time']].apply(milli)
, but all values applied the value of the first row (1:43.702 = 103702) as I use 0 for the first loc parameter. How could I iterate this value so that the results would be: 103702, 104243, 104981 respectively?
By the way, milli is a function that converts the str time into int ms. You can see more about it here.
index | season | round | number | driverId | position | time | ms |
---|---|---|---|---|---|---|---|
0 | 1996 | 1 | 1 | villeneuve | 1 | 1:43.702 | 103702 |
1 | 1996 | 1 | 1 | damon_hill | 2 | 1:44.243 | 103702 |
2 | 1996 | 1 | 1 | irvine | 3 | 1:44.981 | 103702 |
In other words, what should I do to have the value in ms related to the value in time of the same row? Based on https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html I’ve tried also .apply(milli, axis = 1)
, but then I receive following error: “milli() got an unexpected keyword argument ‘axis'”.
Advertisement
Answer
Try:
df_laps['ms'] = df_laps['time'].apply(milli)
or:
df_laps['ms'] = df_laps['time'].apply(lambda x:milli(x))