pandas: duplicate rows from small dataframe to large based on cell value [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-13 21:23:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!