Q:

Pandas division with 2 dfs

I want to divide 2 dfs by matching their names.  For example,
df1 = pd.DataFrame({'Name':['xy-yz','xa-ab','yz-ijk','zb-ijk'],1:[1,2,3,4],2:[1,2,1,2],3:[2,2,2,2]} )
df2 = pd.DataFrame({'Name2':['x','y','z','a'],1:[0,1,2,3],2:[1,2,3,4],3:[5,5,5,6]})
df1:
Name1    1   2   3
xy-yz    1   1   2
xa-ab    2   2   2
yz-ijk   3   1   2
zb-ijk   4   2   2

df2:
Name2   1   2   3
x       0   1   5
y       1   2   5
z       2   3   5
a       3   4   6

The result would be df3:  
Name1    1   2   3
xy-yz    1   1   2
x        0   1   5
xy-yz        1   .4   <---(xy-yz)/x   
xa-ab    2   2   2
x        0   1   5
xa-ab        2   .4    <---(xa-ab)/x
yz-ijk   3   1   2
y        1   2   5
yz-ijk   3   .5  .4    <---(yz-ijk)/y
zb-ijk   4   2   2
z        2   3   5
zb-ijk   2   .67 .4    <---(zb-ijk)/z

I would use concat but I'm not not sure how to do the division by mapping the Name2 to the first letter in Name1 here.  
Thank you!

A:

I do not know why you need it , but this give back what you need 
df2=df2.set_index('Name2')
dfNew=df2.reindex(df1.Name1.str.split('-',expand=True)[0])
df1=df1.set_index('Name1')
pd.concat([df1.reset_index(),dfNew.reset_index().rename(columns={0:'Name1'}),pd.DataFrame(df1.values/dfNew.values,columns=df1.columns).assign(Name1=df1.index)]).sort_index()

Out[897]: 
          1         2    3  Name1
0  1.000000  1.000000  2.0   x-yz
0  0.000000  1.000000  5.0      x
0       inf  1.000000  0.4   x-yz
1  2.000000  2.000000  2.0   x-ab
1  0.000000  1.000000  5.0      x
1       inf  2.000000  0.4   x-ab
2  3.000000  1.000000  2.0  y-ijk
2  1.000000  2.000000  5.0      y
2  3.000000  0.500000  0.4  y-ijk
3  4.000000  2.000000  2.0  z-ijk
3  2.000000  3.000000  5.0      z
3  2.000000  0.666667  0.4  z-ijk

A:

Setup - 
df1 = df1.set_index('Name')
df2 = df2.set_index('Name2')

df1

        1  2  3
Name           
xy-yz   1  1  2
xa-ab   2  2  2
yz-ijk  3  1  2
zb-ijk  4  2  2

df2

       1  2  3
Name2         
x      0  1  5
y      1  2  5
z      2  3  5
a      3  4  6

Retrieve relevant rows from df2 based on the first letter of df1.
i = df2.loc[df1.index.str[0]]
i

       1  2  3
Name2         
x      0  1  5
x      0  1  5
y      1  2  5
z      2  3  5

Perform divison and reset the index.
j = (df1.set_index(i.index) / i).set_index(df1.index)
j
               1         2    3
Name                           
xy-yz        inf  1.000000  0.4
xa-ab        inf  2.000000  0.4
yz-ijk  3.000000  0.500000  0.4
zb-ijk  2.000000  0.666667  0.4

Concatenate the result.
pd.concat([df1, i, j])

               1         2    3
xy-yz   1.000000  1.000000  2.0
xa-ab   2.000000  2.000000  2.0
yz-ijk  3.000000  1.000000  2.0
zb-ijk  4.000000  2.000000  2.0
x       0.000000  1.000000  5.0
x       0.000000  1.000000  5.0
y       1.000000  2.000000  5.0
z       2.000000  3.000000  5.0
xy-yz        inf  1.000000  0.4
xa-ab        inf  2.000000  0.4
yz-ijk  3.000000  0.500000  0.4
zb-ijk  2.000000  0.666667  0.4

Here, once again, use np.isfinite on j if you want to mask non-real numeric values - 
j  = j[np.isfinite(j)].fillna('')
j

        1         2    3
Name                    
xy-yz      1.000000  0.4
xa-ab      2.000000  0.4
yz-ijk  3  0.500000  0.4
zb-ijk  2  0.666667  0.4

Use this j as the argument to concatenation afterwards.

