import pandas as pd
def print_df(dataframe: pd.DataFrame):
print("=================")
print(dataframe)
if __name__ == '__main__':
df = pd.DataFrame([
[0, 1, 1],
[0, 1, 1],
[0, 2, 2],
[0, 2, 3]
], columns=['a', 'b', 'c'])
print("a,b分组:")
df.groupby(["a", "b"]).apply(print_df)
print("c分组:")
df.groupby(["c"]).apply(print_df)
结果:
a,b分组:
=================
a b c
0 0 1 1
1 0 1 1
=================
a b c
2 0 2 2
3 0 2 3
c分组:
=================
a b c
0 0 1 1
1 0 1 1
=================
a b c
2 0 2 2
=================
a b c
3 0 2 3