[英]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 个数据框df和population 。 df df
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另请考虑阅读以下答案以获取有关连接和合并的详细指南
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.