Home > Blockchain >  While creating DataFrame, all the data is appearing in a single row. What is the right method to get
While creating DataFrame, all the data is appearing in a single row. What is the right method to get

Time:10-23

I'm have trying to create DataFrame from stock market api(historical data). The data is appearing well arranged when the api is fetching the result. But when I'm creating DataFrame, all the data is appearing in a single row.

API:

historicParam={
"exchange": "NSE",
"symboltoken": "14977",
"interval": "FIFTEEN_MINUTE",
"fromdate": '2021-07-01 09:41',
"todate": '2021-07-29 10:41'}

a=obj.getCandleData(historicParam)

API response structure is like below(sample). It consists datetime, open, high, low, close and volume values in order.

{'data': [['2021-07-01T09:45:00 05:30', 173.63, 173.7, 173.4, 173.48, 329246],
['2021-07-01T10:00:00 05:30', 173.48, 173.7, 173.29, 173.59, 284357],
['2021-07-01T10:15:00 05:30', 173.55, 173.66, 172.65, 173.14, 549551],
'errorcode': '',
'message': 'SUCCESS',
'status': True}

When I'm using the below line to convert the above data, all the data is showing in a single row in a DataFrame

df = pd.DataFrame([a['data']])
df

The result: Please click here to see the output

What should I do if I want to get data in a DataFrame with open high low close volume columns?

CodePudding user response:

df = pd.DataFrame([a['data']])

You are wrapping your call to a['data'] in a list. Remove the [] and simply pass

df = pd.DataFrame(a['data'])

If you want to name your columns you can pass that as well using;

data = {'data': [['2021-07-01T09:45:00 05:30', 173.63, 173.7, 173.4, 173.48, 329246],
['2021-07-01T10:00:00 05:30', 173.48, 173.7, 173.29, 173.59, 284357],
['2021-07-01T10:15:00 05:30', 173.55, 173.66, 172.65, 173.14, 549551]],
'errorcode': '',
'message': 'SUCCESS',
'status': True}

pd.DataFrame(data['data'], columns=['date', 'open', 'high', 'low', 'close', 'volume'])

                        date    open    high     low   close  volume
0  2021-07-01T09:45:00 05:30  173.63  173.70  173.40  173.48  329246
1  2021-07-01T10:00:00 05:30  173.48  173.70  173.29  173.59  284357
2  2021-07-01T10:15:00 05:30  173.55  173.66  172.65  173.14  549551
  • Related