python/pandas,如何填充持续时间列的缺失值?

发布于 2025-01-21 15:33:35 字数 507 浏览 2 评论 0原文

我的数据(DF)具有一个“ duration_time”列,其值在几分钟和几秒钟内:
10:43

这些值在5到15分钟之间。
此列的数据类型是“对象”。
此列中有几个缺少值。

是否有一种方法可以用本列的平均值填充这些缺失的值?

我尝试了不同的方法,但没有结果。
df ['duration_time'] = df ['duration_time']。fillna(df ['duration_time']。平均值())
我收到了此消息:
只能串联str(不是“ int”)

typeError:当我尝试计算列的平均值时, :
df ['duration_time']。平均()
我收到以下消息:
typeError:只能串联str(不是“ int”)

提前感谢您的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2025-01-28 15:33:35

我认为您的系列DF ['duration_time']实际上不是DateTime类型的。
如果我这样做:

df['duration_time']=pd.Series(['10:43',None,0])
df['duration_time'].fillna(df['duration_time'].mean())

那么我可以重现您的错误。
但是,如果我将该系列包装到to_dateTime()中,则您的代码有效:

df['duration_time']=pd.to_datetime(pd.Series(['10:43',None,0]))
df['duration_time'].fillna(df['duration_time'].mean())

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())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文