Search code examples
pythonpandascsvmultiple-columnsmultiplication

How to multiply two columns with different two csv file and return result in first csv file using Pandas


I have two CSV files where first csv file contains Price column and second csv contains quantity i tried to multiply this two columns and save result in new columns with first csv

First.csv

Code    Description                                Unit    Price
110101  STATIONARY BICYCLE INDOOR USE               SET    120.25
110106  TREADMILL EXERCISE MACHINE, ELEC. AC110V    SET    950.22
110107  TREADMILL EXERCISE MACHINE, ELEC. AC220V    SET    1000 
110110  EXERCISER ROWING INDOOR USE                 SET    450
110120  BARBELL SET                                 SET    100

Second.csv

Code     Quantity
110106  210
110107  220
110110  230
110120  240
110122  250

And the expected output is

First.csv

Code    Description                                 Unit   Price    Total
110101  STATIONARY BICYCLE INDOOR USE               SET    120.25   25252.5
110106  TREADMILL EXERCISE MACHINE, ELEC. AC110V    SET    150.22   33048.4
110107  TREADMILL EXERCISE MACHINE, ELEC. AC220V    SET    100      23000
110110  EXERCISER ROWING INDOOR USE                 SET    40       9600
110120  BARBELL SET                                 SET    100      25000

I'm able to read file only

import pandas as pd

df = pd.read_csv("QuoteCSV.csv", parse_dates=True)
print(df)
df1=pd.read_csv("itemcode.csv",index_col="Price", parse_dates=True)
print(df1)

Updated:

   import pandas as pd

    a = pd.read_csv("itemcode.csv")
    b = pd.read_csv("QuoteCSV.csv")
    b = b.dropna(axis=1)
    merged = a.merge(b, on='Code')
    merged.to_csv("result.csv", index=False)
    c = pd.read_csv("result.csv")
    c['Total'] = c['Price'] * c['Quantity']

But it does not return any rresult


Solution

  • Try This one Work

    df["Total"]=df['Price'].multiply(df1['Quantity'], axis=0)
    print(df)