慕码人2483693
用于带有byGroupBy.cumcount的计数器列:DataFrame.reindexMultiIndex.from_productdf['g'] = df.groupby('A').cumcount()mux = pd.MultiIndex.from_product([df['A'].unique(), range(3)], names=('A','g'))df = (df.set_index(['A','g']) .reindex(mux, fill_value=0) .reset_index(level=1, drop=True) .reset_index())print (df) A B C D0 1 1 3 11 1 2 9 82 1 3 3 93 2 1 2 94 2 2 1 45 2 3 9 56 3 1 4 17 3 2 0 48 3 0 0 09 4 1 2 610 4 0 0 011 4 0 0 012 5 1 2 413 5 2 8 314 5 0 0 0另一种DataFrame.merge使用左连接和助手的解决方案DataFrame:from itertools import productdf['g'] = df.groupby('A').cumcount()df1 = pd.DataFrame(list(product(df['A'].unique(), range(3))), columns=['A','g'])df = df1.merge(df, how='left').fillna(0).astype(int).drop('g', axis=1)print (df) A B C D0 1 1 3 11 1 2 9 82 1 3 3 93 2 1 2 94 2 2 1 45 2 3 9 56 3 1 4 17 3 2 0 48 3 0 0 09 4 1 2 610 4 0 0 011 4 0 0 012 5 1 2 413 5 2 8 314 5 0 0 0编辑:df['g'] = df.groupby(['A','B']).cumcount()mux = pd.MultiIndex.from_product([df['A'].unique(), df['B'].unique(), range(3)], names=('A','B','g'))df = (df.set_index(['A','B','g']) .reindex(mux, fill_value=0) .reset_index(level=2, drop=True) .reset_index())print (df.head(10)) A B C D0 1 1 3 11 1 1 0 02 1 1 0 03 1 2 9 84 1 2 0 05 1 2 0 06 1 3 3 97 1 3 0 08 1 3 0 09 1 4 0 0from itertools import productdf['g'] = df.groupby(['A','B']).cumcount()df1 = pd.DataFrame(list(product(df['A'].unique(), df['B'].unique(), range(3))), columns=['A','B','g'])df = df1.merge(df, how='left').fillna(0).astype(int).drop('g', axis=1)