'Python/Pandas, How to fill missing values of a column of duration times?
My data (df) has a column "duration_time" with values in minutes and seconds like this:10:43
These values range from 5 to 15 minutes.
The data type of this column is 'object'.
There is a couple of missing values in this column.
Is there a way to fill these missing values with the mean of this column?
I have tried different methods but no result.df['duration_time'] = df['duration_time'].fillna(df['duration_time'].mean())
I got this message:TypeError: can only concatenate str (not "int") to str
When I just try to calculate the mean of the column as this:df['duration_time'].mean()
I get the following message:TypeError: can only concatenate str (not "int") to str
Thank you in advance for your help!
Solution 1:[1]
I think your Series df['duration_time'] is not actually of datetime type. If I do:
df['duration_time']=pd.Series(['10:43',None,0])
df['duration_time'].fillna(df['duration_time'].mean())
Then I can reproduce your error. But if I wrap that series into a to_datetime(), then your code works:
df['duration_time']=pd.to_datetime(pd.Series(['10:43',None,0]))
df['duration_time'].fillna(df['duration_time'].mean())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Daniel Weigel |
