Home > Mobile >  How to multiply two dataframes with same structure and same column names containing digits?
How to multiply two dataframes with same structure and same column names containing digits?

Time:09-03

I want to multiply two dataframes (only the column containing digits). Both dataframes contains both digits column and character column. enter image description here

Can anyone suggest a solution to solve this issue?

CodePudding user response:

df1 * df2 , equivalent to df1.multiply(df2) or df1.mul(df2)

df1
###
Name    a  b
Apple   2  3
Orange  4  2

df2
###
Name    a  b
Apple   1  2
Orange  3  3

# multiply df1 and df2
df1 * df2
###
Name     a  b
Apple    2  6
Orange  12  6




If we got different index or column,

df3
### different columns
Name    c  d
Apple   1  2
Orange  3  3

df4
### different indexes
Name     a  b
Banana   1  2
Avocado  3  3

df5
### with other string cols
Name   string_col1 string_col2  a  b
Apple           US    Facebook  1  2
Orange         AUS   Instagram  3  3

Multiply with df1

df1 * df3
###
Name     a   b   c   d
Apple  NaN NaN NaN NaN
Orange NaN NaN NaN NaN


df1 * df4
###
Name      a   b
Apple   NaN NaN
Avocado NaN NaN
Banana  NaN NaN
Orange  NaN NaN

df1 * df5 # same as df5 * df1
###
Name     a  b  string_col1  string_col2
Apple    2  6          NaN          NaN
Orange  12  6          NaN          NaN

For preserving other info, kind of like assigning block,

# take out other info to new dataframe df_out
df_out = df5.loc[:,['string_col1', 'string_col2']]

# take columns would be calculated from df5
# multiply with df1
# assign them to new dataframe df_out whose name are `a` and `b`.
df_out[['a','b']] = df1 * df5[['a', 'b']]

df_out
###
Name   string_col1 string_col2   a  b
Apple           US    Facebook   2  6
Orange         AUS   Instagram  12  6

CodePudding user response:

Sort the values in both the dataframes so that they have same values at same indexes:

cols = ["a", "b"]
(df.sort_values("name")[cols] * df1.sort_values("name")[cols])\
    .assign(name=df.name.sort_values())

    a   b   name
0   2   6   apple
1   12  6   orange
  • Related