I tried to combine and sum 2 Excel by Python but when I used “for loop”, I found an error “unsupported operand type(s) for +: ‘float’ and ‘str'”
See my code below:
import pandas as pd
import os
os.getcwd() + '/projects'
all_bit = os.listdir(os.getcwd() + '/projects/Python_excel/Test_files')
#Read Excel Bitcoin 1
bitcoin1 = pd.read_excel(os.getcwd() + '/projects/Python_excel/Test_files/' + all_bit[0])
#Check file
all_bit[0]
#Change data types
bitcoin1["Latest"] = bitcoin1["Latest"].astype(int)
bitcoin1["Open"] = bitcoin1["Open"].astype(int)
bitcoin1["Highest"] = bitcoin1["Highest"].astype(int)
bitcoin1["Lowest"] = bitcoin1["Lowest"].astype(int)
#Drop unnecessary column
bitcoin1 = bitcoin1.drop(['%Change','Amount'], axis = 1, inplace = False)
#Read Excel Bitcoin 2
bitcoin2 = pd.read_excel(os.getcwd() + '/projects/Python_excel/Test_files/' + all_bit[1])
#Change data types
bitcoin2["Latest"] = bitcoin2["Latest"].astype(int)
bitcoin2["Open"] = bitcoin2["Open"].astype(int)
bitcoin2["Highest"] = bitcoin2["Highest"].astype(int)
bitcoin2["Lowest"] = bitcoin2["Lowest"].astype(int)
#Drop unnecessary column
bitcoin2 = bitcoin2.drop(['%Change','Amount'], axis = 1, inplace = False)
#Loop
rep = pd.DataFrame()
for bitcoin in all_bit:
data_bit = pd.read_excel(os.getcwd() + '/projects/Python_excel/Test_files/' + bitcoin)
rep = rep.add(data_bit.set_index('Date'),fill_value=0)
After I finished my for loop, it showed “TypeError: unsupported operand type(s) for +: ‘float’ and ‘str'”. I tried to change float to int but it not working.
Could you guys advice me?
Thank you