我有两个数据帧,形状为m * 5和5 * n。具有5列的第一个数据帧的列名与具有5行的第二个数据帧的索引相同。我想将第一个数据帧中每一行的每个元素与第二个数据帧中的第一列2列相乘,并在第二个数据帧中具有相应的行索引。请在下面的数据框内查找以供参考:
DataFrame 1:
%age_paid_0.0 %age_paid_0.1 %age_paid_0.2 %age_paid_0.3 \
account_angaza_id
AC005839 0.299221 0.377086 0.454950 0.532814
AC005842 0.299221 0.299221 0.521691 0.521691
AC005843 0.299221 0.377086 0.454950 0.532814
AC005851 0.243604 0.310345 0.354839 0.354839
AC005852 0.299221 0.377086 0.454950 0.532814
AC005853 0.299221 0.377086 0.454950 0.532814
AC005856 0.299221 0.377086 0.454950 0.532814
AC005858 0.299221 0.377086 0.454950 0.532814
AC005859 0.332592 0.432703 0.543938 0.650723
AC005860 0.288098 0.365962 0.421580 0.532814
%age_paid_0.4 %age_paid_0.5
account_angaza_id
AC005839 0.610679 0.688543
AC005842 0.521691 0.521691
AC005843 0.610679 0.766407
AC005851 0.510567 0.555061
AC005852 0.610679 0.766407
AC005853 0.610679 0.688543
AC005856 0.610679 0.766407
AC005858 0.543938 0.588432
AC005859 0.650723 0.739711
AC005860 0.532814 0.632925
数据框2:
0 1
%age_paid_0.0 0.369886 0.673442
%age_paid_0.1 0.409603 0.374386
%age_paid_0.2 0.425269 0.058336
%age_paid_0.3 0.425229 -0.191075
%age_paid_0.4 0.415484 -0.369895
%age_paid_0.5 0.401384 -0.479141
预期的数据框:
0 1
AC005839 1.xxxxxx 2.xxxxxx
AC005840 xxxxxxxx xxxxxxxx
AC005840 xxxxxxxx xxxxxxxx
公式是
dataframe3.loc['AC005839',0] = dataframe1.loc['AC005839',%age_paid_0.1]*dataframe2.loc[%age_paid_0.1,0]+dataframe1.loc['AC005839',%age_paid_0.2]*dataframe2.loc[%age_paid_0.2,0]+...+dataframe1.loc['AC005839',%age_paid_0.5]*dataframe2.loc[%age_paid_0.5,0]
dataframe3.loc['AC005839',1] = dataframe1.loc['AC005839',%age_paid_0.1]*dataframe2.loc[%age_paid_0.1,1]+dataframe1.loc['AC005839',%age_paid_0.2]*dataframe2.loc[%age_paid_0.2,1]+...+dataframe1.loc['AC005839',%age_paid_0.5]*dataframe2.loc[%age_paid_0.5,1]
任何帮助将不胜感激。基本上,我试图将变量转换为与主要组件相同的平面。预先感谢!
答案 0 :(得分:0)
这是一个点积。由于您的索引/列标签匹配,因此您只需:
df1.dot(df2)