问题
I have two sets of numerical data. One is much larger than the other. The same data from the smaller set applies to the larger set multiple times. For example, where B is the data I need to add to the larger set and C is the number of times each value A is referenced in the large set:
Small set:
A B C
123 1 2
456 5 3
Large set:
A D
123 45
123 58
456 32
456 22
456 89
Desired output:
A D B
123 45 1
123 58 1
456 32 5
456 22 5
456 89 5
I have only seen questions wherein people want to remove duplicate fields; here it is important I match the value B so that result D can be better understood.
回答1:
you need, pd.merge
df=pd.merge(df1,df2,on='A')
df=df[['A','D','B']]
来源:https://stackoverflow.com/questions/51573267/pandas-duplicate-rows-from-small-dataframe-to-large-based-on-cell-value