0

I am working on hundreds of excel files through a for loop and generating a new dataframe from each file. At the end, I need to append all the dataframes into one, where only first file should keep the headers. I am using below code. Kindly help me-

folder = r"C:\Users\Mohit\ALGO_TRADING\Play"

for file in os.listdir(folder):
    filepath = os.path.join(folder, file)
    data = pd.read_csv (filepath)
    data['Close_EMA_20'] = ta.EMA(data['Close'],20)
    data['Close_EMA_5'] = ta.EMA(data['Close'],5)
    data ['Position'] = np.nan
    pd.options.mode.chained_assignment = None
    
    for x in range (len (data)):
        if data['Close_EMA_5'].iloc[x] < data['Close_EMA_20'].iloc[x]:
            data ['Position'].iloc[x] = -1
        elif data['Close_EMA_5'].iloc[x] > data['Close_EMA_20'].iloc[x]:
            data ['Position'].iloc[x] = 1
        else:
            data ['Position'].iloc[x] = 0
            
        position_data = data[data['Position']!=0] #creating a new dataframe every after a loop
    
    Position_Data = position_data.append(position_data) #appending all dataframe into one. PROBLEM IS HERE
    
Position_Data.to_csv(r"C:\Users\Mohit\ALGO_TRADING\mohit.csv")
Mohit Garg
  • 61
  • 5

1 Answers1

1

To append two dataframes, use pd.concat([df1, df2])

nathan liang
  • 1,000
  • 2
  • 11
  • 22