将groupby后面的列保留在空数据框中

时间:2017-09-07 07:28:48

标签: python pandas dataframe group-by pandas-groupby

查询后数据帧是一个空的df。当groupby时,提高运行时waring,然后得到另一个没有列的空数据帧。如何保留列?

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

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

Empty DataFrame
Columns: []
Index: []

为什么空数据帧没有列。

runtimewaring:

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

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

1 个答案:

答案 0 :(得分:0)

您需要as_index=Falsegroup_keys=False

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

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

之后无需重置索引。