简体   繁体   中英

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. The code looks like this:

First 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:

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. 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. 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. You can then use the first loop variable to refer to the columns in the second 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. 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. Without the if statement the code terminates with a KeyError as is.

Use a Series in place of df1 and take advantage of index alignement on standard operations:

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

Output:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM