简体   繁体   English

Python - 多次从另一个df中查找一个值

[英]Python - lookup a value from another df multiple times

I have the following two dataframes:我有以下两个数据框:

   prod_id       land_ids
0  1             [1,2]
1  2             [1]
2  3             [2,3,4]
3  4             []
4  5             [3,4]

   land_id       land_desc
0  1             germany
1  2             austria
2  3             switzerland
3  4             italy

Bascially, I want all numbers in column land_ids to individually join the other df.基本上,我希望land_ids 列中的所有数字单独加入另一个df。

The result should look something like this:结果应如下所示:

   prod_id       land_ids  list_land
0  1             [1,2]     germany austria
1  2             [1]       germany
2  3             [2,3,4]   austria switzerland italy
3  4             []     
4  5             [3,4]     switzerland italy

Preferrably, the column list_land is one string where the lands are concatenated.优选地,列list_land 是连接土地的一个字符串。 But I would also be fine with getting a list as a result.但我也可以得到一个列表作为结果。

Any idea on how to do this?关于如何做到这一点的任何想法?

Here is my code for creating the df:这是我创建df的代码:

data_prod = {'prod_id': [1,2,3,4,5], 'land_ids': [[1,2],[1],[2,3,4],[1,3],[3,4]]}
prod_df = pd.DataFrame(data_prod)

data_land = {'land_id': [1,2,3,4], 'land_desc': ['germany', 'austria', 'switzerland', 'italy']}
land_df = pd.DataFrame(data_land)

EDIT: what do I have to add if one value of land_ids is empty?编辑:如果land_ids 的一个值为空,我必须添加什么?

df1 = pd.DataFrame({"prod_id":[1,2,3,4,5],"land_ids":[[1,2],[1],[2,3,4],[1,3],[3,4]]})
df2 = pd.DataFrame({"land_id":[1,2,3,4],"land_ids":["germany","austria","switzerland","italy"]})

df2 = df2.set_index('land_id', drop=True)
df1['list_land'] = df1['land_ids'].apply(lambda x: [df2.at[ids, 'land_desc'] for ids in x])

If you want to get list_land as a string, than you can do like this.如果您想将list_land作为字符串获取,则可以这样做。

df1['list_land'] = df1['land_ids'].apply(lambda x: " ".join([df2.at[ids, 'land_desc'] for ids in x]))

you can use the apply method:你可以使用apply方法:

prod_df['list_land'] = prod_df['land_ids'].apply(lambda x: [land_df.loc[land_df['land_id'] == y]['land_ids'].values[0] for y in x])

In this case, the list_land column is a list.在这种情况下, list_land列是一个列表。 You can use the following code if you want it to be a string.如果您希望它是一个字符串,您可以使用以下代码。

prod_df['list_land'] = prod_df['land_ids'].apply(lambda x: ' '.joind([land_df.loc[land_df['land_id'] == y]['land_ids'].values[0] for y in x]))

Maybe something like this:也许是这样的:

import pandas as pd 


df1 = pd.DataFrame({"prod_id":[1,2,3,4,5],"land_ids":[[1,2],[1],[2,3,4],[1,3],[3,4]]})
df2 = pd.DataFrame({"land_id":[1,2,3,4],"land_ids":["germany","austria","switzerland","italy"]})

list_land = []

for index, row in df1.iterrows():
    list_land.append([row2.land_ids for land_id in row["land_ids"] for _, row2 in df2.iterrows() if row2.land_id == land_id])
df1["list_land"] = list_land

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

相关问题 Python Pandas查找并替换df2中的df1值 - Python Pandas lookup and replace df1 value from df2 Python panda 从另一个df中搜索df中的值 - Python panda search for value in a df from another df Python-在另一个df中查找一个单元格的值,返回一个boolean的值给另一个单元格 - Python-Lookup a cell value in another df, return a boolean value to another cell 如何向 dataframe (df1) 添加一个新列,这是另一个 dataframe (df2) 中 df1 的多个查找值的总和 - How can I add a new column to a dataframe (df1) that is the sum of multiple lookup values from df1 in another dataframe (df2) 合并/查找到另一个 df - merge/lookup to another df 将匹配值从一个 df 复制到另一个给定的多个条件 - Copy matching value from one df to another given multiple conditions python pandas数据帧-无法弄清楚如何在给定df值的情况下查找索引 - python pandas dataframe - can't figure out how to lookup an index given a value from a df 熊猫:使用地图功能查找另一个df中的值 - Pandas: use map function to LOOKUP a value in another df 查找一个值,如果它存在于另一个 df 中,则在两个新列中返回文本 - Lookup a value and if it is present in another df, return text in two new columns Python / Pandas:如果有匹配项,则将值从一个df添加到另一df的行末 - Python/Pandas: add value from one df to end of row in another df if there is a match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM