Pandas groupby TypeError:“ SeriesGroupBy”和“ int”的实例之间不支持“>”

时间:2019-10-11 11:29:07

标签: python-3.x pandas dataframe group-by

我有以下代码:

group = df.groupby(['Id',  'Name'])
conditions = [
    ((group["Balance"]>0) & (group['Value']>0) & (group['L_Date']<=group['c_date'])),
    ((group["Balance"]==0) & (group['Value']==0) & (group['L_Date']<=group['c_date']))]
choices = ['Good', 'Bad']
df['outcome'] = np.select(conditions, choices, default='Normal')

但这给了我以下错误:

TypeError: '>' not supported between instances of 'SeriesGroupBy' and 'int'

正确的方法是什么?

1 个答案:

答案 0 :(得分:3)

我认为没有理由使用groupby,因为在代码中没有按组计算某些值。

group = df.groupby(['Id',  'Name'])

仅用于:

conditions = [
    ((df["Balance"]>0) & (df['Value']>0) & (df['L_Date']<=df['c_date'])),
    ((df["Balance"]==0) & (df['Value']==0) & (df['L_Date']<=df['c_date']))]
choices = ['Good', 'Bad']
df['outcome'] = np.select(conditions, choices, default='Normal')