简体   繁体   English

如何根据第一个数据帧列中的值从另一个数据帧添加新列?

[英]How to add new column from another dataframe based on values in column of first dataframe?

Guys I have 2 dataframes df and population .伙计们,我有 2 个数据框dfpopulation df df

population人口

I simply want to add new column named Population to df and to add population numbers from dataframe population to this column.我只想将名为Population 的新列添加到df并将数据框人口中的人口数添加到此列。 I want to have population data next to each city.我想在每个城市旁边都有人口数据。

How can I manage this ?我该如何管理? Any advices?有什么建议吗?

Ideal Solution理想的解决方案

Product Province    Quantity  Population
PRODUCT_A   ankara  16          5663322
PRODUCT_A   ankara  25          5663322
PRODUCT_A   ankara  56          5663322
PRODUCT_A   ankara  16          5663322
PRODUCT_A   adana   11          2258718
PRODUCT_A   adana   25          2258718
PRODUCT_A   ankara  35          5663322
PRODUCT_A   adana   54          2258718
PRODUCT_A   adana   17          2258718
PRODUCT_A   adana   30          2258718
PRODUCT_A   adana   12          2258718
PRODUCT_A   ankara  18          5663322
PRODUCT_A   ankara  14          5663322
PRODUCT_A   ankara  21          5663322
PRODUCT_A   aydin   16          1119084

This is done via a join operation which in pandas can be done with .merge() .这是通过join操作完成的,在 pandas 中可以使用.merge()

Kindly try using the following:请尝试使用以下方法:

df = df.merge(population,how='left',on='Province')

Also please consider reading the following answer for adetailed guide on joins and merges另请考虑阅读以下答案以获取有关连接和合并详细指南

use join in dataframe.在数据框中使用连接。 use this link使用这个链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据 Pandas 中第一个数据框中的某些列值从第二个数据框中添加列值 - How to add Column Values from 2nd DataFrame Based on Some Column Values in First dataframe in Pandas 根据条件从另一个数据帧的值向数据帧添加新列 - Adding a new column to a dataframe from the values of another dataframe based on a condition 根据来自另一个 DataFrame 的信息,将新列添加到 DataFrame - Add new column to DataFrame based on information from another DataFrame 根据来自另一个数据框的值将列添加到数据框 - Add column to dataframe based on values from another dataframe 如何根据另一个 dataframe 的匹配为 dataframe 的新列添加值? - how to add value to a new column to a dataframe based on the match of another dataframe? 如何使用另一个数据框添加数据框并基于列更新公共值 - how to add a dataframe with another dataframe and updated common values based on a column 向 pandas dataframe 添加一个新列,其中包含来自另一列的转换值? - Add a new column to pandas dataframe with coverted values from another column? 如何在 dataframe 的中间添加一个新列,其值基于上一列? - How to add a new column in the middle of the dataframe with values based on the previous column? Python根据另一个数据框中的多个条件添加新的列值 - Python add new column values based on multiple conditions in another dataframe 如何将数据从一个 dataframe 添加到另一个 dataframe 上的新列 - how to add data from one dataframe to a new column on another dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM