Home > database >  How to create an empty dataframe and append to it without creating a new dataframe object?
How to create an empty dataframe and append to it without creating a new dataframe object?

Time:06-21

I'm using the binance websocket to stream close data to a dataframe, and I need to be able to have an updated dataframe from those closes. But, with everything I try, pandas just creates a new dataframe object.

if candle_closed:
     close = candle['c']

     df = pd.DataFrame()
     df = pd.concat((pd.DataFrame([close]), df), axis=0)

df
                0
0  20431.65000000
                0
0  20439.63000000

What I need:

                0
1  20431.65000000
2  20439.63000000

CodePudding user response:

Okay, if you have an existing dataframe with "close" data, and you're getting a string from the websocket, then you need to use pd.Series() to first put the string into a Series object and then use pd.concat() with ignore_index=True:

In [2]: df
Out[2]:
                0
0  20431.65000000

In [3]: c
Out[3]: '20439.63000000'

In [4]: df = pd.concat([df, pd.Series(c)], ignore_index=True)

In [5]:
Out[5]:
                0
0  20431.65000000
1  20439.63000000
  • Related