Multiplying values between csv documents with the same date
Question:
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!
Answers:
import csv
csv_1_path = r"C:UsersWX847AppDataRoamingJetBrainsPyCharmCE2022.1scratchescsv1.csv"
csv_2_path = r"C:UsersWX847AppDataRoamingJetBrainsPyCharmCE2022.1scratchescsv2.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)
csv_1_date_dict = {row[0].strip(): [row[1].strip(), row[2].strip()] for row in cvs_1_data_list}
csv_2_date_dict = {row[0].strip(): [row[1].strip()] for row in cvs_2_data_list}
for k_date in csv_2_date_dict:
if k_date in csv_1_date_dict:
one_res = float(csv_1_date_dict[k_date][0])*float(csv_1_date_dict[k_date][1]) * float(csv_2_date_dict[k_date][0])
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
156609.8255184
"""
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.
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!
import csv
csv_1_path = r"C:UsersWX847AppDataRoamingJetBrainsPyCharmCE2022.1scratchescsv1.csv"
csv_2_path = r"C:UsersWX847AppDataRoamingJetBrainsPyCharmCE2022.1scratchescsv2.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)
csv_1_date_dict = {row[0].strip(): [row[1].strip(), row[2].strip()] for row in cvs_1_data_list}
csv_2_date_dict = {row[0].strip(): [row[1].strip()] for row in cvs_2_data_list}
for k_date in csv_2_date_dict:
if k_date in csv_1_date_dict:
one_res = float(csv_1_date_dict[k_date][0])*float(csv_1_date_dict[k_date][1]) * float(csv_2_date_dict[k_date][0])
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
156609.8255184
"""
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.