将分组后的列保留在空数据框中 [英] Keep columns after a groupby in an empty dataframe

查看:39
本文介绍了将分组后的列保留在空数据框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查询后该数据框为空df.当进行groupby时,发出运行时警告,然后又得到另一个无列的空数据框.如何保留这些列?

The dataframe is an empty df after query.when groupby,raise runtime waring,then get another empty dataframe with no columns.How to keep the columns?

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

结果:

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

然后分组依据:

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

结果: 有时是无 有时是空的数据框

result: sometimes is None sometime is empty dataframe

Empty DataFrame
Columns: []
Index: []

为什么空数据框没有列.

why empty dataframe has no columns.

运行时警告:

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

如果alpha + beta * ngroups< count * np.log(count):

if alpha + beta * ngroups < count * np.log(count):

/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):

推荐答案

您需要as_index=Falsegroup_keys=False:

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

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

此后无需重置索引.

这篇关于将分组后的列保留在空数据框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆