Python Groupbyapply重复执行

2024-10-04 01:22:15 发布

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

为什么下面的代码有3次执行?我只希望它执行两次,每个组一次(foo和bar),在bar上有两次执行。你知道吗

import pandas as pd

df = pd.DataFrame({'type' : ['foo', 'bar'], 'cost' : [1, 4]})


def testFunction(currDf):
    print currDf
    return currDf

grouped_df = df.groupby(['type']).apply(testFunction)

grouped_df

cost type
1     4  bar
cost type
1     4  bar
cost type
0     1  foo

Tags: 代码importdataframepandasdffoodefas