宝慕林4294392
在这种情况下,您可以使用 groupBy。import pandas as pdimport numpy as npdf = pd.DataFrame(np.random.randint(15,size =(30,2)),columns= ["ID", "Sub ID"])df.sort_values(by=["ID", "Sub ID"], ignore_index =True, ascending=True, inplace=True)df2 = df.groupby(['ID', 'Sub ID']).size().reset_index(name ='Total Amount')print(df2) 让我知道