Python:如何从数据帧中行查找数据,并根据列匹配在另一个数据帧中相乘

时间:2017-12-08 15:23:20

标签: python pandas numpy dataframe lookup

我有一个数据框df1,它具有以下结构

A   B   C   D
10  9   9   4
5   4   4   9
5   10  6   4
9   9   9   4
4   7   10  7
9   7   4   8
5   7   8   9
10  4   10  6

我有另一个数据帧df2如下,

name    factor
A   2
B   3
C   4
D   8

如何从df2查找A,B,C,D的值并乘以df1 [A,B,C,D]并得到df3,例如,

A   B   C   D
20  27  36  32
10  12  16  72
10  30  24  32
18  27  36  32
8   21  40  56
18  21  16  64
10  21  32  72
20  12  40  48

1 个答案:

答案 0 :(得分:1)

mul创建由set_index创建的多个for i in range(81): if i == 80: ab += str(a) + '*' + str(b) + '<>' if i in (30, 60, 80): ab = ab.rstrip('<>') my_list += my_func(ab) ab = '' ab += str(a) + '*' + str(b) + '<>'

Series

详情:

df3 = df1.mul(df2.set_index('name')['factor'])
print (df3)
    A   B   C   D
0  20  27  36  32
1  10  12  16  72
2  10  30  24  32
3  18  27  36  32
4   8  21  40  56
5  18  21  16  64
6  10  21  32  72
7  20  12  40  48

编辑:

如果缺少某些类别,请使用print (df2.set_index('name')['factor']) name A 2 B 3 C 4 D 8 Name: factor, dtype: int64 ,感谢Wen

fillna

EDIT1:

如果想要按s = df2.set_index('name').drop('D')['factor'] print (s) name A 2 B 3 C 4 Name: factor, dtype: int64 df3 = df1.mul(s).fillna(df1) print (df3) A B C D 0 20.0 27.0 36.0 4.0 1 10.0 12.0 16.0 9.0 2 10.0 30.0 24.0 4.0 3 18.0 27.0 36.0 4.0 4 8.0 21.0 40.0 7.0 5 18.0 21.0 16.0 8.0 6 10.0 21.0 32.0 9.0 7 20.0 12.0 40.0 6.0 比较DataFrame

Series

对于所有列:

s = df2.set_index('name')['factor']
print (s)
name
A    2
B    3
C    4
D    8
Name: factor, dtype: int64

df1['A'] = (np.log(df1['A']) * s['A']) ** 3
print (df1)
           A   B   C  D
0  97.664572   9   9  4
1  33.351293   4   4  9
2  33.351293  10   6  4
3  84.862013   9   9  4
4  21.313578   7  10  7
5  84.862013   7   4  8
6  33.351293   7   8  9
7  97.664572   4  10  6