简体   繁体   English

与python索引匹配

[英]Index match with python

I have two dfs 我有两个DFS

df1 DF1

 Len    Bar
 x      a
 y      a
 z      a
 x      b
 y      b
 z      b
 x      c
 y      c
 z      c

df2 DF2

Len/Bar a   b   c
x       4   2   8
y       2   7   7
z       6   3   9

Need output to be 需要输出

Len Bar Amount
x   a   4
y   a   2
z   a   6
x   b   2
y   b   7
z   b   3
x   c   8
y   c   7
z   c   9

In excel I use index match formula =INDEX($B$2:$D$4,MATCH(A19,$A$2:$A$4,0),MATCH(B19,$B$1:$D$1,0)) 在Excel中,我使用索引匹配公式=INDEX($B$2:$D$4,MATCH(A19,$A$2:$A$4,0),MATCH(B19,$B$1:$D$1,0))

But is there any way to do the same using map or merge 但是有没有办法使用map或merge做同样的事情

I think you need first reshape df2 and then merge with left join with df1 : 我认为您需要首先重塑df2 ,然后与df1左连接merge

df2 =df2.set_index('Len/Bar').unstack().rename_axis(('Bar','Len')).reset_index(name='Amount')
df2 = df1.merge(df2, how='left', on=['Len', 'Bar'])
print (df2)
  Len Bar  Amount
0   x   a       4
1   y   a       2
2   z   a       6
3   x   b       2
4   y   b       7
5   z   b       3
6   x   c       8
7   y   c       7
8   z   c       9

Another solution: 另一个解决方案:

df2 = df2.set_index('Len/Bar').stack().rename_axis(('Bar','Len')).rename('Amount')
df2 = df1.join(df2, on=['Len', 'Bar'])
print (df2)
  Len Bar  Amount
0   x   a       4
1   y   a       2
2   z   a       6
3   x   b       2
4   y   b       7
5   z   b       3
6   x   c       8
7   y   c       7
8   z   c       9

EDIT: 编辑:

If you dont know if need merge/join it depends if need filter reshaped df2 by df1 or not. 如果您不知道是否需要合并/连接,则取决于是否需要通过df1将过滤器重塑df2

See difference: 看到差异:

#removed some rows
print (df1)
  Len Bar
0   x   a
1   y   a
2   z   a
3   x   b
4   y   b

print (df2)
  Bar Len  Amount
0   a   x       4
1   a   y       2
2   a   z       6
3   b   x       2
4   b   y       7
5   b   z       3
6   c   x       8
7   c   y       7
8   c   z       9

And after merge rows are filtered by columns Len and Bar from df1 : merge行之后,通过df1 LenBar列对行进行过滤:

print (df3)
  Len Bar  Amount
0   x   a       4
1   y   a       2
2   z   a       6
3   x   b       2
4   y   b       7

Incidentally, you do not seem to need df1 at all: 顺便说一句,您似乎根本不需要df1

df3 = df2.set_index('Len/Bar').stack().reset_index()
df3.columns = "Len", "Bar", "Amount"
#  Len Bar Amount
#0   x   a      4
#1   x   b      2
#2   x   c      8
#3   y   a      2
#4   y   b      7
#5   y   c      7
#6   z   a      6
#7   z   b      3
#8   z   c      9

Unless you want to borrow the column names from it: 除非您想从中借用列名:

df3.columns = df1.columns + ("Amount",)

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

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