Home > other >  How can i add more dataset data in already exist dataset in pandas?
How can i add more dataset data in already exist dataset in pandas?

Time:12-03

I have an output csv format excel file in which I want to add more data by using pandas. I created a output file but after I want to add more data in that output file so how can I do this?

This is my code for creating output file and I don't want to change anything in this code-

output_df = pd.DataFrame(columns=['Symbol', 'Date', 'Entry_time', 'Exit_time', 'Multiplier', 
                                              'Round-Off', 'Stop_loss %', 'Minimum_Expiry', 'GapUp/GapDown',
                                              'Entry_price_ce', 'Stop_loss_ce', 'Stop_loss_price_ce', 'Exit_time_ce', 'Exit_price_ce',
                                              'Entry_price_pe', 'Stop_loss_pe', 'Stop_loss_price_pe', 'Exit_price_pe', 'Exit_time_pe',
                                              'PNL_ce', 'PNL_pe'])
                    c = 0
                    output_df.loc[c]=[symbol, date, entry_time, exit_time, multiplier, roundoff, stop_loss_pr,
                                      min_expiry, gap, entry_price_ce, stop_loss_ce, stop_loss_price_ce, exit_time_ce, 
                                      exit_price_ce, entry_price_pe, stop_loss_pe, stop_loss_price_pe, exit_price_pe, exit_time_pe,
                                      pnl_ce, pnl_pe]
                    mergefile.append(output_df)
                
            
        
            DT_Sell = pd.concat(mergefile)
            df = DT_Sell.drop_duplicates(subset='Date', keep="first")
            df.reset_index(drop=True, inplace=True)
            display(df)
            csv_path = Path(r'C:\Users\krishna gupta\Global_trader\My Codes\DT\CSV\DT_Buy')
    
            starting_time = entry_time.strftime("%H-%M-%S")
            end_time = exit_time.strftime("%H-%M-%S")
    
            df.to_csv(csv_path / f'DT_Buy_{starting_time}_{end_time}_{stop_loss_pr}_{multiplier}_{roundoff}.csv', index=False)
        

There is any way I can append my new output from my code in my old csv output file? which I created by the same code.

CodePudding user response:

To append to a csv file, use df.to_csv(filename, mode='a')

  • Related