python - 如何根据列值乘以 2 个不相等的数据框?

标签 python pandas dataframe

我有 2 个数据框。如何根据特定列乘以 2 个不相等的数据帧。是否有 pandas 包或者我需要使用循环。有人可以分享将两个尺寸不等的数据帧相乘的代码吗?

df1 is below:

    features   tf_vals
0       this  0.200000
1         is  0.200000
2        the  0.200000
3      first  0.200000
4   document  0.200000
5       this  0.166667
6   document  0.333333
7         is  0.166667
8        the  0.166667
9     second  0.166667
10       and  0.166667
11      this  0.166667


df2 is :

   features  idf_vals
0       the  1.000000
1      this  1.000000
2  document  1.000000
3        is  1.000000
4     first  1.510826
5       one  1.916291
6    second  1.916291
7       and  1.916291
8     third  1.916291

How do I multiply df1 and df2 based on column name 'features'.

Desired output:
I want to multiply value of word in df1 and value of word in df2. example(value of word 'this' in df1 * value of word 'this' in df2 )

features   Mult
this       0.2
is         0.2
the        0.2
first      0.3021652
document   0.2
this       0.166667
document   0.333333
is         0.166667
the        0.166667
second     0.319382472
and        0.319382472
this       0.166667

最佳答案

想法是使用 Series.map 通过df1['features']然后乘以 Series.mul 避免对 features 进行排序栏目:

s = df2.set_index('features')['idf_vals']
df1['Mult'] = df1['features'].map(s).mul(df1.pop('tf_vals'))

print (df1)
    features      Mult
0       this  0.200000
1         is  0.200000
2        the  0.200000
3      first  0.302165
4   document  0.200000
5       this  0.166667
6   document  0.333333
7         is  0.166667
8        the  0.166667
9     second  0.319382
10       and  0.319382
11      this  0.166667

关于python - 如何根据列值乘以 2 个不相等的数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59227284/

相关文章:

python - 从 pandas qcut 间隔中删除小数点(将间隔转换为整数)

python - 支持哪些源包文件类型?

python - 将 Pandas 与 IN 语句的元组一起使用时转义单引号

python - 如何根据另一个数据框中的条件在数据框中创建新列?

python - 如何根据另一列中的值在数据框中设置零和一

python - Pandas 数据框两列的交集

python - 使用python提供应用服务

python - Pandas:根据唯一值获取行中对应的列值

python - 查找数据区间并对其进行排序

python - 如何将不同的功能应用于 groupby 对象?