在空datafram中保留groupby后面的列

2024-10-01 17:28:42 发布

您现在位置:Python中文网/ 问答频道 /正文

数据帧是一个空的数据框查询.whengroupby,引发运行时警告,然后获取另一个没有列。怎么保留柱子?在

df = pd.DataFrame(columns=["PlatformCategory","Platform","ResClassName","Amount"])
print df

结果:

^{pr2}$

然后是groupby:

df = df.groupby(["PlatformCategory","Platform","ResClassName"]).sum()
df = df.reset_index(drop=False,inplace=True)
print df

结果: 有时候没有 有时是空数据帧

Empty DataFrame
Columns: []
Index: []

为什么空数据帧没有列。在

运行时警告:

/data/pyrun/lib/python2.7/site-packages/pandas/core/groupby.py:3672: RuntimeWarning: divide by zero encountered in log

如果alpha+beta*n组<;count*np.日志(计数):

/data/pyrun/lib/python2.7/site-packages/pandas/core/groupby.py:3672: RuntimeWarning: invalid value encountered in double_scalars
  if alpha + beta * ngroups < count * np.log(count):

Tags: 数据警告dataframedfdatalibpackagescount
1条回答
网友
1楼 · 发布于 2024-10-01 17:28:42

您需要as_index=False和{}:

df = df.groupby(["PlatformCategory","Platform","ResClassName"], as_index=False).count()
df

Empty DataFrame
Columns: [PlatformCategory, Platform, ResClassName, Amount]
Index: []

之后不需要重置索引。在

相关问题 更多 >

    热门问题