简体   繁体   English

基于来自另一个 DataFrame 的列乘以行值

[英]Multiplying row values based on column from another DataFrame

I'm trying to change the values in a DataFrame based on the columns of another DataFrame.我正在尝试根据另一个 DataFrame 的列更改 DataFrame 中的值。 The code looks like this:代码如下所示:

First dataframe:首先dataframe:

df1 = pd.DataFrame({'Ticker':['M2NS Index', 'ECMSM2 Index','A23 VWYH Index'], 'Factor':[4,3,2]})
df1
    Ticker          Factor
0   M2NS Index           4
1   ECMSM2 Index         3
2   A23 VWYH Index       2

Second dataframe:第二个 dataframe:

df2 = pd.DataFrame({'M2NS Index':[5,2,1], 'ECMSM2 Index':[5,2,1], 'A23 VWYH Index':[5,2,1]})
df2
    M2NS Index  ECMSM2 Index    A23 VWYH Index
0   5           5               5
1   2           2               2
2   1           1               1

I'm want to multiply the row values with 10^factor, where the factor is in the first table.我想将行值乘以 10^因子,其中因子在第一个表中。 Different columns will multiply with the associated factor.不同的列将与相关的因子相乘。 My resulting frame would look like:我得到的框架看起来像:

df3 = pd.DataFrame({'M2NS Index':[50000,20000,10000], 'ECMSM2 Index':[5000,2000,1000], 'A23 VWYH Index':[500,200,100]})
df3
    
    M2NS Index  ECMSM2 Index    A23 VWYH Index
0   50000       5000            500
1   20000       2000            200
2   10000       1000            100

If anyone has any idea on how to multiply without using location but rather indexing that would be great.如果有人对如何在不使用位置而是索引的情况下进行乘法有任何想法,那就太好了。 The order of the columns in the second dataframe might be different from the order of the rows in the first dataframe.第二个 dataframe 中的列顺序可能与第一个数据帧中的行顺序不同。 Any help would be appreciated!任何帮助,将不胜感激!

You can use to_records(index=False) to convert the data in a pair of columns to a list of tuples for easy iteration with a for loop.您可以使用to_records(index=False)将一对列中的数据转换为元组列表,以便使用for循环进行迭代。 You can then use the first loop variable to refer to the columns in the second DataFrame.然后,您可以使用第一个循环变量来引用第二个 DataFrame 中的列。

import pandas as pd

df1 = pd.DataFrame({'Ticker':['M2NS Index', 'ECMSMS2 Index','A23 VWYH Index'], 'Factor':[4,3,2]})
df2 = pd.DataFrame({'M2NS Index':[5,2,1], 'ECMSM2 Index':[5,2,1], 'A23 VWYH Index':[5,2,1]})
# Make a copy of df2
df3 = df2

# Iterate over pairs of 'Ticker', 'Factor' from df1 rows
for ticker, factor in df1[['Ticker', 'Factor']].to_records(index=False):
    # If there is a column in df2 with the same name as 'Ticker'
    if ticker in df2.columns:
        # Multiply by the factor of ten specified
        df3[ticker] = df2[ticker] * 10 ** factor

Outputs:输出:

       M2NS Index  ECMSM2 Index  A23 VWYH Index
0           50000             5             500
1           20000             2             200
2           10000             1             100

What happened to the second column, you ask?你问第二栏怎么了? You have ECMSMS2 Index written in df1 but ECMSM2 Index (missing the second 'S') in df2.您在 df1 中编写了ECMSMS2 Index ,但在 df2 中编写了ECMSM2 Index (缺少第二个“S”)。 You'll need to take care to ensure that the names match, or insert some kind of handling after the if statement to match if x% of letters match or something like that.您需要注意确保名称匹配,或者在if语句之后插入某种处理以匹配 x% 的字母匹配或类似的东西。 Without the if statement the code terminates with a KeyError as is.如果没有if语句,代码将按原样以KeyError终止。

Use a Series in place of df1 and take advantage of index alignement on standard operations:使用 Series 代替df1并在标准操作中利用索引对齐:

df3 = df2 * 10**df1.set_index('Ticker')['Factor']

Output: Output:

   M2NS Index  ECMSM2 Index  A23 VWYH Index
0       50000          5000             500
1       20000          2000             200
2       10000          1000             100

暂无
暂无

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

相关问题 Pandas:将从 DataFrame 中提取的值乘以另一个 DataFrame 中的列值 - Pandas: Multiplying a value extracted from a DataFrame to column values in another DataFrame 根据另一个 dataframe 的匹配行和列填充 dataframe 中的值 - Populate values in a dataframe based on matching row and column of another dataframe 根据另一个datarame熊猫中的行将数据框中的行相乘 - multiplying rows in dataframe based on row in another datarame pandas 将NumPy数组中的行乘以基于另一行的特定值 - Multiplying row in NumPy array by specific values based on another row 使用 pandas 根据来自另一个 dataframe 的行值填充列值 - fill column values based on row values from another dataframe using pandas 根据相似的列值在单独的熊猫数据框中乘以列 - Multiplying columns in separate pandas dataframe based on similar column values 根据 Python 中另一个 dataframe 的行值从 dataframe 中获取列? - Taking columns from a dataframe based on row values of another dataframe in Python? for 循环,用于根据 Python 中的条件将每一行的值与另一个表中的特定列相乘 - for Loop for multiplying a value of each row with a specific column from another table based on condition in Python Pandas 基于另一个 dataframe 将多个列和行值设置为 nan - Pandas Set multiple column and row values to nan based on another dataframe 将数据框乘以一列…但是值是字符串吗? - Multiplying a dataframe by a column… but values are strings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM