我有两个CSV文件,其中第一个csv文件包含Price列,第二个csv包含数量我试图将这两列相乘并使用第一个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
预期的输出是
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
我只能阅读文件
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)
更新:
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']
但它没有返回任何结果
答案 0 :(得分:0)
使用map
First.assign(
Total=First.Price * First.Code.map(dict(zip(Second.Code, Second.Quantity))))
答案 1 :(得分:0)
尝试这个工作
df["Total"]=df['Price'].multiply(df1['Quantity'], axis=0)
print(df)