Python Pandas Dataframe检查列表列并从另一个Dataframe返回ID

时间:2017-04-15 18:04:12

标签: python pandas numpy list-comprehension melt

我有一个pandas数据帧df1,它有一个索引和一列列表,如下所示:

index   IDList
0   [1,3,5,7]
1   [2,4,5,8]
2   [6,8,9]
3   [1,2]

我有另一个pandas数据帧df2,它有NewID作为索引,还有一列列表如下所示:

NewID   IDList
1       [3]
2       [4,5]
3       [1,7]
4       [2]
5       [9,3]
6       [8]
7       [6]

我需要做的是df1.IDList中是否存在df2.IDList中的任何项目,然后返回相关df2.NewID的列表。

因此,返回的d1数据框将如下所示:

index   IDList      NewID
0       [1,3,5,7]   [3,1,2,3,5]
1       [2,4,5,8]   [4,2,2,6]
2       [6,8,9]     [7,6,5]
3       [1,2]       [3,4]

编辑请注意,在df2中, IDList 中可能存在多行中的ID(请参阅 ID 3) df1.IDList以及 ID 3在df2行1和5中显示的位置

我在考虑某种np.where声明,其中包含了any&#39;和列表理解?但不确定如何申请IDList中的每个df1,其中df2.IDList查看整个.stack()。也许某种.melt()?还是$(document).ready(function(){ $.ajax({ type: "GET", url: 'http://www.dais.unive.it/~cosmo/esercitazione3/captcha.php?getIdentifier', timeout: 2000 }).done(function(data){ var obj = JSON.parse(data); var session_id = obj.id; $("#test").append("Session id: "+ session_id + "<br/>"); }).fail(function(){ alert("Error"); }); }); ?在vlookup为df2 ...

的电子表格中,这很容易

帮助表示赞赏......

1 个答案:

答案 0 :(得分:1)

# expand and map ids from IDList to NewID
flat_ids = pd.DataFrame({
    "NewID": pd.np.repeat(df2.NewID, df2.IDList.str.len().tolist()),
    "IDList": [x for l in df2.IDList for x in l]
}).set_index("IDList").NewID

# extract ids from flat ids using loc
df1['NewID'] = df1['IDList'].map(lambda x: flat_ids.loc[x].tolist())

enter image description here