以下代码显示regroup
被传递了原始数据帧的子帧。该子框架具有一列books
,但是当函数regroup
试图调用df.groupby('books')时,即使在groupby之前的行打印了数据帧,也会引发错误。所以我的问题是:
一个人如何遍历groupby(expr)
的子帧,以便人们可以将这些子帧传递给另一个函数,并且该函数还可以在该子帧上调用groupby(expr)
,其中{{ 1}}首次通话和后续通话是否完全相同?
expr
答案 0 :(得分:1)
您可以这样做:
def regroup(df):
section("regroup")
print(f" input df={df}")
gb = df.groupby('books')
# differences lie here
for book, group in gb:
_df = pd.DataFrame(group)
print(_df)
endsection("regroup")
输出:
<BEGIN regroup>
input df= books b c
0 abc 2 -5
1 def 5 2
2 abc 3 1
3 xyz 2 8
4 def 6 2
books b c
0 abc 2 -5
2 abc 3 1
books b c
1 def 5 2
4 def 6 2
books b c
3 xyz 2 8
<END regroup>