我想通过银行与下面显示的两列mean
来计算standard deviation
的timedelta
和dataframe
。当我运行代码(也显示在下面)时,出现以下错误:
pandas.core.base.DataError: No numeric types to aggregate
我的数据框:
bank diff
Bank of Japan 0 days 00:00:57.416000
Reserve Bank of Australia 0 days 00:00:21.452000
Reserve Bank of New Zealand 55 days 12:39:32.269000
U.S. Federal Reserve 8 days 13:27:11.387000
我的代码:
means = dropped.groupby('bank').mean()
std = dropped.groupby('bank').std()
参考方案
您需要将timedelta
转换为某个数字值,例如int64
用values
最准确,因为ns
的数字表示形式是转换为timedelta
:
dropped['new'] = dropped['diff'].values.astype(np.int64)
means = dropped.groupby('bank').mean()
means['new'] = pd.to_timedelta(means['new'])
std = dropped.groupby('bank').std()
std['new'] = pd.to_timedelta(std['new'])
另一种解决方案是通过 seconds
将值转换为total_seconds
,但准确性较低:
dropped['new'] = dropped['diff'].dt.total_seconds()
means = dropped.groupby('bank').mean()
在 Pandas 中放置多列 - python我正在尝试使用以下代码在熊猫数据框中按索引号删除多列(数据集中的第2列和第70列,分别索引为1和69):df.drop([df.columns[[1, 69]]], axis=1, inplace=True) 我收到以下错误:TypeError: unhashable type: 'Index' 在我的代码中,[1,69]突出显示并说:E…
在 Pandas 图中仅隐藏轴标签,而不是整个轴 - python我可以使用以下命令清除Pandas图中xlabel的文本:plt.xlabel("") 而是可以隐藏标签吗?可能类似于.xaxis.label.set_visible(False)。 参考方案 From the Pandas docs- Series和DataFrame上的plot方法只是对plt.plot()的简单包装:这意味着您可以使…
Python Pandas:按分组分组,平均? - python我有一个像这样的数据框:cluster org time 1 a 8 1 a 6 2 h 34 1 c 23 2 d 74 3 w 6 我想计算每个集群每个组织的平均时间。预期结果:cluster mean(time) 1 15 ((8+6)/2+23)/2 2 54 (74+34)/2 3 6 我不知道如何在熊猫中做到这一点,有人可以帮忙吗? 参考方案 如…
Python Pandas:在多列上建立布尔索引 - python尽管至少有关于如何在Python的pandas库中为DataFrame编制索引的two good教程,但我仍然无法在一个以上的列上找到一种优雅的SELECT编码方式。>>> d = pd.DataFrame({'x':[1, 2, 3, 4, 5], 'y':[4, 5, 6, 7, 8]}) >…
python pandas:按行对条件进行分组 - python我有一个大的pandas数据框,试图从中形成一些行的对。我的df如下所示:object_id increment location event 0 1 d A 0 2 d B 0 3 z C 0 4 g A 0 5 g B 0 6 i C 1 1 k A 1 2 k B ... ... ... ... 对象ID描述特定的对象。增量是每次发生某事(跟踪订单)时…