How to read the below data to a dataframe in python

Question:

I have the data in the below variable
ohlc =
1664788799|38444.9|38569.2|38327.85|38412.35|0|0|,1664789099|38408.35|38587.3|38394.05|38586.15|0|0|,1664789399|38589.55|38641.6|38420.05|38422.35|0|0|

The coulums are timestamp, open, high, low, close, volume,OI

How do we convert this pythion variable to dataframe. Any simple way to to do this?

Asked By: user3698161

||

Answers:

could you please give a demo of output, in which way you want to (like table)

Split the string into lists using the two separators and then pass the list of lists to the dataframe constructor:

pd.DataFrame(
    [row.split('|')[:-1] for row in ohlc.split(',')],
    columns=['timestamp', 'open', 'high', 'low', 'close', 'volume', 'OI']
)

Output:

     timestamp      open       high      low       close    volume  OI
0   1664788799   38444.9    38569.2 38327.85    38412.35         0   0
1   1664789099  38408.35    38587.3 38394.05    38586.15         0   0
2   1664789399  38589.55    38641.6 38420.05    38422.35         0   0
Answered By: AlexK
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.