Chris
Chris

Reputation: 181

Multiplying Pandas DataFrames with Column and Index Names

I have two pandas dataframes that I am importing from excel workbooks. The have item names as the in the first column and Months as the column headers. They are the same size and have identical column headers/row index names. I would like to multiply these two dataframes together and end up with the same item names in the first column and the same months in the header, but the product of the arrays in the cells.

I have tried: df * df1,df.values * df1.values, and df.mul(df1) and every time I get

TypeError: can't multiply sequence by non-int of type 'str'

an abbreviated example of my data is:

df:

                  SKU                       2017-01-31   2017-02-28 
0                FUJI Film                     0.761031   0.838831

df1:

                SKU                    2017-01-31          2017-02-28                   
 0          FUJI  FILM                  8557.12              7240.64

I imported the files with the following:

df = pd.read_excel('C:/desktop/Arrays.xlsx',sheet_name=0)
df1 = pd.read_excel('C:/desktop/Arrays.xlsx',sheet_name=1)

and I would ideally like to end up with a df3 that is the product of the two arrays while the first column and the headers remain the same.

I appreciate any help.

Upvotes: 0

Views: 339

Answers (1)

BENY
BENY

Reputation: 323396

You can using select_dtypes to exclude the non numeric type columns , then we using mul , and update , to get the result and update the value accordingly

df.update(df.select_dtypes(exclude='object').mul(df1.select_dtypes(exclude='object')))
df
Out[891]: 
        SKU   2017-01-31   2017-02-28
0  FUJIFilm  6512.233591  6073.673292

Upvotes: 1

Related Questions