Home > Mobile >  How can I convert .append to .concat panda python
How can I convert .append to .concat panda python

Time:03-12

I have a current iteration to fill new rows to a dataframe based on new series created:

    current = self.getAllCandles(self.active_id,start_candle)
    main = pd.DataFrame()
    useful_frame = pd.DataFrame()
    for candle in current:
        useful_frame = pd.DataFrame(list(candle.values()),index = list(candle.keys())).T.drop(columns = ['at'])
        #useful_frame['from'] = datetime.datetime.fromtimestamp(int(useful_frame['from'])).strftime('%Y-%m-%d %H:%M:%S')
        useful_frame = useful_frame.set_index(useful_frame['from']).drop(columns = ['id'])
        main = main.append(useful_frame)
        main.drop_duplicates()
    final_data = main.drop(columns = {'to'})
    final_data = final_data.loc[~final_data.index.duplicated(keep = 'first')]

Since df.append() will be deprecated, I'm struggling to execute the same instructions using df.concat(). But I'm not getting it, how could I change that?

CodePudding user response:

Create an empty python list and then append all the series to the list. Finally call pandas' concat on that list, this will give you that dataframe.

CodePudding user response:

I think this is what you're looking for:

current = self.getAllCandles(self.active_id, start_candle)
frames = []
for candle in current:
    useful_frame = pd.DataFrame.from_dict(candle, orient='columns')
    #useful_frame['from'] = datetime.datetime.fromtimestamp(int(useful_frame['from'])).strftime('%Y-%m-%d %H:%M:%S')
    useful_frame = useful_frame.set_index('from')
    useful_frame = useful_frame.drop(columns=['at', 'id'])
    frames.append(useful_frame)

main = pd.concat(frames).drop_duplicates()
final_data = main.drop(columns='to')
final_data = final_data.loc[~final_data.index.duplicated()]
  • Related