简体   繁体   English

如何将 dataframe 中的每一行乘以不同 dataframe 的不同列,并将所有行的总和作为 Python 中的新列?

[英]How to multiply each row in dataframe by a different column of different dataframe and get sum of all rows as a new column in Python?

I am trying to multiply each row of a dataframe by values of a column in another dataframe,我试图将 dataframe 的每一行乘以另一个 dataframe 中一列的值,

For example if I have the following dataframe:例如,如果我有以下 dataframe:

df = pd.DataFrame({
     'FR': [4.0405, 4.0963, 4.3149],
     'GR': [1.7246, 1.7482, 1.8519],
     'IT': [804.74, 810.01, 860.13],
     'F':  [8.4, 10.01, 60.3]},
     index=['1980-01-01', '1980-02-01', '1980-03-01'])
df_ret = df.pct_change()

df2 = pd.DataFrame({'symbol':['FR','GR','IT','F'],
                    'weight' : [0.2,0.3,0.1,0.4]})

I want to multiply each element of each row in df_ret to df2['weight']我想将 df_ret 中每一行的每个元素乘以 df2['weight']

Then find the sum of each row and populate in a new column as df['port']然后找到每行的总和并填充到一个新列中作为 df['port']

I have tried:我努力了:

df_ret.mul(df2.weight)

but I got a larger dataframe with all values as NaN但我得到了一个更大的 dataframe,所有值为 NaN

any help is appreciated, thanks.任何帮助表示赞赏,谢谢。

We can take advantage of index alignment by setting the index ( set_index ) of df2 to symbol and multiplying df_ret by the newly indexed df2['weight'] aligning Series index with df_ret columns.我们可以通过将df2的索引 ( set_index ) 设置为symbol并将df_ret乘以新索引的df2['weight']使系列索引与df_ret列对齐来利用索引 alignment。 Then we can sum across the rows ( axis=1 ) with DataFrame.sum :然后我们可以用DataFrame.sum对行 ( axis=1 ) 求和:

df_ret = df_ret.mul(df2.set_index('symbol')['weight'])
df_ret['Port'] = df_ret.sum(axis=1)

df_ret : df_ret

                  FR        GR        IT         F      Port
1980-01-01       NaN       NaN       NaN       NaN  0.000000
1980-02-01  0.002762  0.004105  0.000655  0.076667  0.084189
1980-03-01  0.010673  0.017795  0.006188  2.009590  2.044246

We can also set skipna=False if we want NaN in the first row instead of 0:如果我们想在第一行中使用NaN而不是 0,我们也可以设置skipna=False

df_ret['Port'] = df_ret.sum(axis=1, skipna=False)
                  FR        GR        IT         F      Port
1980-01-01       NaN       NaN       NaN       NaN       NaN
1980-02-01  0.002762  0.004105  0.000655  0.076667  0.084189
1980-03-01  0.010673  0.017795  0.006188  2.009590  2.044246

暂无
暂无

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

相关问题 将一个 dataframe 的每一行乘以第二个 dataframe 中列的所有行 - Multiply each row of one dataframe by all rows of column in second dataframe 如何将一行中的所有元素与 pandas dataframe 中的 Python 中的同一行中的相应元素相乘? - How to multiply all elements in a row with corresponding element in same row in different column in pandas dataframe in Python? 将列的每个元素乘以同一数据框中不同列的每个元素 - Multiply each element of a column by each element of a different column in same dataframe 如何在 dataframe 的不同列中对具有相同键的列的行求和? - How to sum rows of a column with same key in a different column of the dataframe? 如何将 dataframe 中的每一列与另一个 dataframe pandas 的行相乘? - How to multiply each column in a dataframe with a row from another dataframe pandas? 将列的每个元素乘以不同数据帧的每个元素 - Multiply each element of a column by each element of a different dataframe 将一行中的每个元素相乘并在同一数据框中追加新列? - Multiply each element in one row and append new column in same dataFrame? Python根据不同的行创建新的数据框列 - Python create new dataframe column based on different rows 基于不同行条件的新 dataframe 列 - New dataframe column based on conditionals of different rows dataframe 中的质心按行排列,并将每列乘以不同的质量 - Centre of mass row-wise in a dataframe and multiply each column by different mass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM