Pandas Groupby ValueError:DataFrame的真值是不明确的

时间:2015-10-08 02:21:57

标签: python pandas

我使用Pandas的read_sql_query函数从SQL查询创建一个新的DataFrame。

在遍历DataFrame时,有几个维度列我们使用groupby函数,它通常有效。但是,对于某些列,它不会,而是我得到一个错误。错误是:

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

在我尝试复制该问题时,我创建了一个dict并将其转换为DataFrame。这个新的DataFrame还具有唯一的值(字符串)。 groupby函数可以正常工作。似乎可能如果DataFrame是从sql查询创建的,那么它与groupby有关吗?因为我不能在查询之外重现我们的数据库(PostgreSQL)的问题,所以我不能在这里运行任何可运行的东西。下面是我用来从SQL

创建DataFrame的代码
cur = conn.cursor()
query ="""SELECT * from table1""" 
df = pd.read_sql_query(query, conn)
df['date'] = pd.to_datetime(df['date'])
df = df.set_index(pd.DatetimeIndex(df['date'])) 

我不认为我在做上述事情有什么疯狂。

引用:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-410-83c5166962e7> in <module>()
      5 
      6 df1 = df.drop(['client_name', 'class_type', 'message', 'from_name', 'from_id'], axis=1)
----> 7 df1.groupby('post_id').sum()
      8 
      9 #             current_data = current_data.set_index(dim)

/Users/Dev/anaconda/lib/python2.7/site-packages/pandas/core/generic.pyc in groupby(self, by, axis, level, as_index, sort, group_keys, squeeze)
   3157         axis = self._get_axis_number(axis)
   3158         return groupby(self, by=by, axis=axis, level=level, as_index=as_index,
-> 3159                        sort=sort, group_keys=group_keys, squeeze=squeeze)
   3160 
   3161     def asfreq(self, freq, method=None, how=None, normalize=False):

/Users/Dev/anaconda/lib/python2.7/site-packages/pandas/core/groupby.pyc in groupby(obj, by, **kwds)
   1197         raise TypeError('invalid type: %s' % type(obj))
   1198 
-> 1199     return klass(obj, by, **kwds)
   1200 
   1201 

/Users/Dev/anaconda/lib/python2.7/site-packages/pandas/core/groupby.pyc in __init__(self, obj, keys, axis, level, grouper, exclusions, selection, as_index, sort, group_keys, squeeze)
    386         if grouper is None:
    387             grouper, exclusions, obj = _get_grouper(obj, keys, axis=axis,
--> 388                                                     level=level, sort=sort)
    389 
    390         self.obj = obj

/Users/Dev/anaconda/lib/python2.7/site-packages/pandas/core/groupby.pyc in _get_grouper(obj, key, axis, level, sort)
   2152             in_axis, name = False, None
   2153 
-> 2154         if is_categorical_dtype(gpr) and len(gpr) != len(obj):
   2155             raise ValueError("Categorical dtype grouper must have len(grouper) == len(data)")
   2156 

/Users/Dev/anaconda/lib/python2.7/site-packages/pandas/core/generic.pyc in __nonzero__(self)
    712         raise ValueError("The truth value of a {0} is ambiguous. "
    713                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
--> 714                          .format(self.__class__.__name__))
    715 
    716     __bool__ = __nonzero__

ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

0 个答案:

没有答案