简体   繁体   English

Python,Pandas 合并 x 和 y 并用 y 中的 z 填充

[英]Python, Pandas Merge x and y and fill with z from y

I'm not even sure this is possible but it most certainly is worth asking.我什至不确定这是否可能,但绝对值得一问。 This would be a rather simple task in Excel however I'm finding it extremely difficult in Pandas.这在 Excel 中将是一项相当简单的任务,但我发现在 Pandas 中非常困难。

I have DF1:我有 DF1:

| | Date |日期 | Location ID |位置 ID |

| | -------- | -------- | -------------- | -------------- |

| | DD-MM-YYY | DD-MM-YYY | 1 | 1 |

| | DD-MM-YYY | DD-MM-YYY | 2 | 2 | (120k Rows Total) (总共 120k 行)

I have DF2:我有 DF2:

|Date | |日期 | Location ID |位置 ID | Location |位置 |

|:---- |:------:| |:---- |:-----:| -----:| -----:|

| | DD-MM-YYY | DD-MM-YYY | 1 | 1 | India |印度 | (4 Rows Total) - 4 different locations (共 4 行) - 4 个不同的位置

I want to merge the DFs together on ['Location ID'] and then auto-fill DF1 Location row with all the correct worded locations.我想在 ['Location ID'] 上将 DF 合并在一起,然后用所有正确的位置自动填充 DF1 Location 行。 So add the column Location to all the 120k rows based upon the Location ID.因此,根据 Location ID 将列 Location 添加到所有 120k 行中。

Basically New DF1: |Date |基本全新的 DF1:|日期 | Location ID |位置 ID | Location |位置 |

|:---- |:------:| |:---- |:-----:| -----:| -----:|

| | DD-MM-YYY | DD-MM-YYY | 1 | 1 | India |印度 | (120K times) (12万次)

Thanks in advance.提前致谢。 If this is possible that would be great.如果这是可能的,那就太好了。

import pandas as pd

df1 = pd.DataFrame({'Date': ['01-01-1999', '02-01-1999'],
                    'Location ID': [1, 2]})

df2 = pd.DataFrame({'Date': ['01-01-1999', '02-01-1999'], 
                    'Location ID': [1, 2],
                    'Location': ['India', 'Pakistan']})

df3 = pd.merge(left=df1, right=df2[['Location ID', 'Location']], how='left', on='Location ID')

print(df3)

gives

         Date  Location ID  Location
0  01-01-1999            1     India
1  02-01-1999            2  Pakistan

PS I'd suggest reading up on this excellent thread on how to make reproducible pandas examples . PS我建议阅读这个关于如何制作可重现的熊猫示例的优秀主题。 It will get you better responses here.它会让你在这里得到更好的回应。 ;) ;)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM