I have 2 csv documents which looks as follows:
csv_1, For example:
2021-01-04, 137.03, 133.21
2021-12-13, 141.61, 133.48
2021-01-07, 143.3, 133.48
csv_2, For example:
2021-01-04, 8.1881
2021-01-05, 8.2144
2021-01-07, 8.1876
What I want to accomplish is to have the values 137.03 and 133.21 multiplied with 8.1881 and do this with every item in the csv documents.
I attempted to open the documents in a readable state, and with indexes and a for loop, I tried to accomplish this task. I would greatly appreciate any help on this!
CodePudding user response:
import csv
csv_1_path = r"C:\Users\WX847\AppData\Roaming\JetBrains\PyCharmCE2022.1\scratches\csv1.csv"
csv_2_path = r"C:\Users\WX847\AppData\Roaming\JetBrains\PyCharmCE2022.1\scratches\csv2.csv"
cvs_1_data_list = []
with open(csv_1_path, 'r', encoding='utf-8') as f1:
reader1 = csv.reader(f1)
for row in reader1:
cvs_1_data_list.append(row)
print(cvs_1_data_list)
cvs_2_data_list = []
with open(csv_2_path, 'r', encoding='utf-8') as f2:
reader2 = csv.reader(f2)
for row in reader2:
cvs_2_data_list.append(row)
print(cvs_2_data_list)
for row_id, row in enumerate(cvs_1_data_list):
one_res = float(row[1].strip()) * float(row[2].strip()) * float(cvs_2_data_list[row_id][1].strip())
print(one_res)
"""
[['2021-01-04', ' 137.03', ' 133.21'], ['2021-12-13', ' 141.61', ' 133.48'], ['2021-01-07', ' 143.3', ' 133.48']]
[['2021-01-04', ' 8.1881'], ['2021-01-05', ' 8.2144'], ['2021-01-07', ' 8.1876']]
149463.66384103004
155269.43324031998
156609.8255184
"""
CodePudding user response:
You can use pandas DataFrame to simplify the steps:
create two dataframe for 2 csv files (You can store the value in different dataframe)
import pandas as pd
df = pd.read_csv('csv_1.csv', names=['Date', 'A', 'B'], header=None))
df1 = pd.read_csv('csv_2.csv', names=['Date', 'C'], header=None))
used print statement to see/print the value
print(df[["A", "B"]].multiply(df1["C"], axis="index"))
if you want to store the value you can store in new dataframe
df2 = df[["A", "B"]].multiply(df1["C"], axis="index")
note : header - None is used to trim column names is already exists in CSV file.