Vlookup 来自另一个数据框的元素,用于在 Python 中创建 MultiIndex DataFrame
Vlook Up Elements from another dataframe for creating MultiIndex DataFrame in Python
有 2 个 Dataframes 和 1 个分层索引(pandas multiIndex)。
数据框 A 有一个 ID 和名称列表。
Dataframe B 有一个名称组合列表和一个相似度分数。
我想基于 multiindex 从 Dataframe A 中获取值并检查 DataFrame B 中是否存在该组合。如果是,我想将相似度分数带到我的 Multiindex dataframe 中,否则仅为 0。
DataFrame A(原始数据框)
test= pd.DataFrame({'row':['a','b','c','d'],'col_A' : ["Alexis","Alexi","Peter","Pete"]})
test = test.set_index('row');test
Out:
row col_A
a Alexis
b Alexi
c Peter
d Pete
DataFrame B(名称相似度)
names = pd.DataFrame({'A' : ["Alexis","Alexi","Peter","Pete"]
,'B' : ["Alexi","Alexis","Pete","Peter"]
, "similarity" : [0.9,0.9,0.8,0.8]})
Out:
A B similarity
0 Alexis Alexi 0.9
1 Alexi Alexis 0.9
2 Peter Pete 0.8
3 Pete Peter 0.8
多索引
# Creating a Pandas MultiIndex
arrays = [['a', 'a', 'a', 'b', 'b', 'c'],
['b', 'c', 'd', 'c', 'd', 'd']]
tuples = list(zip(*arrays))
indexy = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
遍历索引我使用下面的函数,但是,我不确定如何调整它,以便在存在时带来相似度分数或在不存在时带来 0。
a = pd.DataFrame((test.reindex(indexy.get_level_values(0)).values (?) test.reindex(indexy.get_level_values(1))).values,index=indexy,columns=test.columns)
我想看起来像这样的地方:
row similarity
first second
a b 0.9
c 0
d 0
b c 0
d 0
c d 0.8
因此,如果您对多索引不太感兴趣,可以通过以下方式获取您期望的数据:
import pandas as pd
test= pd.DataFrame({'row':['a','b','c','d'],'col_A' : ["Alexis","Alexi","Peter","Pete"]})
names = pd.DataFrame({'A' : ["Alexis","Alexi","Peter","Pete"],
'B' : ["Alexi","Alexis","Pete","Peter"],
"similarity" : [0.9,0.9,0.8,0.8]})
注意我没有 set_index
test
但你可以做到,它会稍微改变以下内容(见评论)。您可以创建 a
数据框 a,例如:
import itertools
a = pd.DataFrame([p for p in itertools.combinations(test['col_A'], 2)],columns =['A','B'],
index=['%s,%s'%p for p in itertools.combinations(test['row'], r=2)])
# here if you did set_index your test, then replace
# index=['%s,%s'%p for p in itertools.combinations(test['row'], r=2)] by
# index=['%s,%s'%p for p in itertools.combinations(test.index, r=2)]
它看起来像:
A B
a,b Alexis Alexi
a,c Alexis Peter
a,d Alexis Pete
b,c Alexi Peter
b,d Alexi Pete
c,d Peter Pete
然后你可以使用 reset_index
(将当前索引作为列获取,但这取决于你想要什么)merge
和 A 和 B 列上的 names
,填充 nan
为 0,删除 A 和 B 两列,并重命名(如有必要):
a = a.reset_index().merge(names, how = 'left', on = ['A','B']).fillna(0).\
drop(labels = ['A','B'], axis=1).rename(columns = {'index':'row', 'similarity':'col_A'})
让我知道你是否可以在之后做你想做的事
编辑:使用您寻找的新输出,您可以:
a = pd.DataFrame([p for p in itertools.combinations(test['col_A'], 2)],columns =['A','B'],
index=pd.MultiIndex.from_tuples([p for p in itertools.combinations(test.index, r=2)], names=['first', 'second']))
注意:itertools
生成在 pd.MultiIndex.from_tuples
中用于定义多索引 DF 的元组。
现在你可以merge
(为了保持多索引,你需要reset_index
之前和set_index
之后:
a = a.reset_index().merge(names, how = 'left', on = ['A','B']).fillna(0).\
drop(labels = ['A','B'], axis=1).set_index(['first', 'second'])
这是使用多索引 merge
和 map
的另一种方法:
from itertools import combinations
a = pd.DataFrame(index = pd.MultiIndex.from_tuples(list(combinations(test.col_A,2))))
a = a.merge(names, left_index=True, right_on=['A','B'],how='left').fillna(0)
testmap = test.reset_index().set_index('col_A').squeeze()
a['A'] = a.A.map(testmap)
a['B'] = a.B.map(testmap)
a = a.set_index(['A','B'])
a
输出:
similarity
A B
a b 0.9
c 0.0
d 0.0
b c 0.0
d 0.0
c d 0.8
详情
- 使用 itertools 的组合创建 MultiIndex
- 将具有多索引的空数据帧合并到 'names' 数据帧并用零填充 NaN
- 使用 set_index 创建一个系列以将 'col_A' 映射回测试 'row' 中的值
有 2 个 Dataframes 和 1 个分层索引(pandas multiIndex)。 数据框 A 有一个 ID 和名称列表。 Dataframe B 有一个名称组合列表和一个相似度分数。
我想基于 multiindex 从 Dataframe A 中获取值并检查 DataFrame B 中是否存在该组合。如果是,我想将相似度分数带到我的 Multiindex dataframe 中,否则仅为 0。
DataFrame A(原始数据框)
test= pd.DataFrame({'row':['a','b','c','d'],'col_A' : ["Alexis","Alexi","Peter","Pete"]})
test = test.set_index('row');test
Out:
row col_A
a Alexis
b Alexi
c Peter
d Pete
DataFrame B(名称相似度)
names = pd.DataFrame({'A' : ["Alexis","Alexi","Peter","Pete"]
,'B' : ["Alexi","Alexis","Pete","Peter"]
, "similarity" : [0.9,0.9,0.8,0.8]})
Out:
A B similarity
0 Alexis Alexi 0.9
1 Alexi Alexis 0.9
2 Peter Pete 0.8
3 Pete Peter 0.8
多索引
# Creating a Pandas MultiIndex
arrays = [['a', 'a', 'a', 'b', 'b', 'c'],
['b', 'c', 'd', 'c', 'd', 'd']]
tuples = list(zip(*arrays))
indexy = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
遍历索引我使用下面的函数,但是,我不确定如何调整它,以便在存在时带来相似度分数或在不存在时带来 0。
a = pd.DataFrame((test.reindex(indexy.get_level_values(0)).values (?) test.reindex(indexy.get_level_values(1))).values,index=indexy,columns=test.columns)
我想看起来像这样的地方:
row similarity
first second
a b 0.9
c 0
d 0
b c 0
d 0
c d 0.8
因此,如果您对多索引不太感兴趣,可以通过以下方式获取您期望的数据:
import pandas as pd
test= pd.DataFrame({'row':['a','b','c','d'],'col_A' : ["Alexis","Alexi","Peter","Pete"]})
names = pd.DataFrame({'A' : ["Alexis","Alexi","Peter","Pete"],
'B' : ["Alexi","Alexis","Pete","Peter"],
"similarity" : [0.9,0.9,0.8,0.8]})
注意我没有 set_index
test
但你可以做到,它会稍微改变以下内容(见评论)。您可以创建 a
数据框 a,例如:
import itertools
a = pd.DataFrame([p for p in itertools.combinations(test['col_A'], 2)],columns =['A','B'],
index=['%s,%s'%p for p in itertools.combinations(test['row'], r=2)])
# here if you did set_index your test, then replace
# index=['%s,%s'%p for p in itertools.combinations(test['row'], r=2)] by
# index=['%s,%s'%p for p in itertools.combinations(test.index, r=2)]
它看起来像:
A B
a,b Alexis Alexi
a,c Alexis Peter
a,d Alexis Pete
b,c Alexi Peter
b,d Alexi Pete
c,d Peter Pete
然后你可以使用 reset_index
(将当前索引作为列获取,但这取决于你想要什么)merge
和 A 和 B 列上的 names
,填充 nan
为 0,删除 A 和 B 两列,并重命名(如有必要):
a = a.reset_index().merge(names, how = 'left', on = ['A','B']).fillna(0).\
drop(labels = ['A','B'], axis=1).rename(columns = {'index':'row', 'similarity':'col_A'})
让我知道你是否可以在之后做你想做的事
编辑:使用您寻找的新输出,您可以:
a = pd.DataFrame([p for p in itertools.combinations(test['col_A'], 2)],columns =['A','B'],
index=pd.MultiIndex.from_tuples([p for p in itertools.combinations(test.index, r=2)], names=['first', 'second']))
注意:itertools
生成在 pd.MultiIndex.from_tuples
中用于定义多索引 DF 的元组。
现在你可以merge
(为了保持多索引,你需要reset_index
之前和set_index
之后:
a = a.reset_index().merge(names, how = 'left', on = ['A','B']).fillna(0).\
drop(labels = ['A','B'], axis=1).set_index(['first', 'second'])
这是使用多索引 merge
和 map
的另一种方法:
from itertools import combinations
a = pd.DataFrame(index = pd.MultiIndex.from_tuples(list(combinations(test.col_A,2))))
a = a.merge(names, left_index=True, right_on=['A','B'],how='left').fillna(0)
testmap = test.reset_index().set_index('col_A').squeeze()
a['A'] = a.A.map(testmap)
a['B'] = a.B.map(testmap)
a = a.set_index(['A','B'])
a
输出:
similarity
A B
a b 0.9
c 0.0
d 0.0
b c 0.0
d 0.0
c d 0.8
详情
- 使用 itertools 的组合创建 MultiIndex
- 将具有多索引的空数据帧合并到 'names' 数据帧并用零填充 NaN
- 使用 set_index 创建一个系列以将 'col_A' 映射回测试 'row' 中的值