Home > OS >  Map Dataframe Columns to Another Dataframe
Map Dataframe Columns to Another Dataframe

Time:04-02

I have a function mult that multiplies a number by 100, 200, 300, 400, and 500, and returns a dataframe with 1 row and 5 columns. mult(1):

     col1    col2    col3    col4    col5
0    100     200     300     400     500

I have a data frame df1 with x columns and y rows, and one of the columns contains a number:

     cola    colb  colc
0    asdf    b     1
1    axci    c     2
2    vhsk    r     3

I want to use map to call my function mult on colc of df1 and add the results as new columns:

     cola    colb  colc   col1    col2    col3    col4    col5
0    asdf    b     1      100     200     300     400     500
1    axci    c     2      200     400     600     800     1000
2    vhsk    r     3      300     600     900     1200    1500

The code I try to use for this is:

df1[['col1','col2','col3','col4','col5']] = df1['colc'].map(mult).

When I run the code, I get an error stating Columns must be same length as key. From my understanding, that error occurs when the number of columns on the right hand side is greater than the number of columns I'm trying to assign, but I've confirmed there are 5 columns on both sides.

Any insight as to what I'm doing wrong, or how to get the mapping to work would be appreciated.

CodePudding user response:

If you refer to a general function that returns a dataframe, I think this works:

df1[['col1','col2','col3','col4','col5']] = pd.concat(df1['colc'].map(mult).to_list(), ignore_index=True)

CodePudding user response:

Just perfect for numpy array broadcasting:

df1[mult.columns] = mult.to_numpy() * df1["colc"].to_numpy()[:, None]

CodePudding user response:

Try this:

df2.join(pd.DataFrame(df2['colc'].to_numpy()[:,None] * df1.to_numpy(), 
             columns=df1.columns, 
             index=df2.index))

Output:

   cola colb  colc  col1  col2  col3  col4  col5
0  asdf    b     1   100   200   300   400   500
1  axci    c     2   200   400   600   800  1000
2  vhsk    r     3   300   600   900  1200  1500
  • Related