简体   繁体   English

如何将数据从一个 dataframe 添加到另一个 dataframe 上的新列

[英]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.

相关问题 如何将一列从一个数据帧添加到另一个数据帧? - How can I add a column from one dataframe to another dataframe? 如何向现有 dataframe 添加新列并用另一列的部分数据填充它? - How do I add a new column to an existing dataframe and fill it with partial data from another column? 如何根据键列将新行从 dataframe 添加到另一行 - How can I add new rows from a dataframe to another one based on key column 根据来自另一个 DataFrame 的信息,将新列添加到 DataFrame - Add new column to DataFrame based on information from another DataFrame 如何在 pyspark 中从另一个 dataframe 添加列? - How to add column to one dataframe from another in pyspark? 如何根据第一个数据帧列中的值从另一个数据帧添加新列? - How to add new column from another dataframe based on values in column of first dataframe? 如何用另一个数据帧更新一个熊猫数据帧(更新旧数据并添加新数据) - How to update one pandas dataframe with another dataframe (update the old data and add new data) 如何根据另一个 dataframe 的匹配为 dataframe 的新列添加值? - how to add value to a new column to a dataframe based on the match of another dataframe? 将一个 dataframe 中的行与另一个 dataframe 中的匹配列相乘并添加 - multiply row from one dataframe with matching column in another dataframe and add 将列数据从一个数据帧转置到另一个数据帧 - Transpose column data from one dataframe to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM