[英]how to add data from one dataframe to a new column on another dataframe
I got a dataframe of albums rating and I'm trying to add a column that will state the number of albums the artist produced (number of 'artist' duplicates in the df)我得到了专辑评分的 dataframe 并且我正在尝试添加一个列,该列将 state 艺术家制作的专辑数量(df中“艺术家”重复的数量)
I created a separate df with 2 columns 'artist' and 'number_of_albums' and thought I could then add the value of 'number_of_albums' to the original df according to the artist in each row.我创建了一个单独的 df,其中包含 2 列 'artist' 和 'number_of_albums' 并认为我可以根据每行中的艺术家将 'number_of_albums' 的值添加到原始 df 中。
dups_artists = df.pivot_table(columns=['artist'], aggfunc='size')
artists_df = pd.DataFrame({'artist':dups_artists.index, 'number_of_albums':dups_artists.values})
but I'm not sure how to do that, also it seems like there must be a simpler way to achieve the result...但我不知道该怎么做,而且似乎必须有一种更简单的方法来实现结果......
left = df.set_index('artist')
right = df2.set_index('artist')
left.join(right)
or you can use on='artist' when joining.或者您可以在加入时使用 on='artist' 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.