pythonazureazure-billing-api

Azure Python Billing API


I am trying to generate a report using python for Azure billing API. i am getting the response from WEBAPI in csv format. with follwoing code:

import requests
import pandas as pd
url=  
"https://consumption.azure.com/v2/enrollments/"+xxxxx+"/usagedetails/download? 
startTime="+startTime+"&endTime="+endTime
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36',
      'Authorization': 'Bearer {0}'.format(key)}
response=requests.get(url,headers=headers)
response_format=response.data.decode('utf-8-sig')
TESTDATA1=StringIO(response_format)
df1=pd.DataFrame([sub.split(",") for sub in TESTDATA1])

Now I have a question. where is my data what has column values but the column values are coming as 0,1,2,3 like . The actual column values are going into the 2nd row of the data-frame. how to fix this ?any leads will be highly appreciated.


Solution

  • This is due to how you are initializing your DataFrame.

    You can added the column names by doing

    df1.columns = df1.values[the row with column names]
    

    then drop that row