简体   繁体   English

如果值在另一个 dataframe 的另一列中,则添加列

[英]Add column if value is in another column of another dataframe

One of my dataframe is:我的 dataframe 之一是:

     name    value
0    Harry     a
1    Kenny     b
2    Zoey      h

another is:另一个是:

     list                    topic
0    Jame, Harry, Noah      topic1
1    lee, zee               topic2  

I want if any of the names of dataframe1 is in list of dataframe2 it should add a name column 'present' in dataframe1 with the values as of the respective topic.我想如果 dataframe1 的任何名称在 dataframe2 的列表中,它应该在 dataframe1 中添加一个名称列“存在”,其中包含相应主题的值。

     name    value   present
0    Harry     a      topic1
1    Kenny     b       none
2    Zoey      h       none

UPDATED QUERY更新查询

df1 df1

     name        value
0    Harry Lee     a
1    Kenny         b
2    Zoey          h

df2 is same and desired result is df2 相同,期望的结果是

     name    value   present
0 Harry Lee    a      topic1 topic2
1    Kenny     b       none
2    Zoey      h       none

We need to trim the df1 with explode then we can do map我们需要用explode修剪 df1 然后我们可以做map

df1['list'] = df1['list'].str.split(',')
s = df1.explode('list')
df['present'] = df.name.map(dict(zip(s['list'],s['topic'])))
df
Out[550]: 
    name value present
0  Harry     a  topic1
1  Kenny     b     NaN
2   Zoey     h     NaN
import pandas as pd
import numpy as np

df1 = pd.DataFrame({"name":['Harry', 'Kenny', 'Zoey'], "value":["a", "b", "h"]})
df2 = pd.DataFrame({"list": ["Jame, Harry, Noah", "lee, zee"], "topic": ["topic1", "topic2"]})

def add_column(x):
    try:
        present = df2[df2['list'].str.contains(x)].iloc[0,1]
    except IndexError:
        present = np.NAN
    return present
df1['present'] = df1['name'].apply(add_column)

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

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