Search code examples
pythondataframeazure-blob-storageetldata-transfer

Saving a DF to azure blob


I am trying to save a df which is returned from a function (return df). I am trying to push this to my azure blob storage account.

I am having some troubles as all the solutions I have found required a file path, however I just want to run some code on a dataframe and save it automatically to azure blob.

As per requests, a snippet of my code :)

As stated above, I am looking to save the df (a pandas dataframe) as a .csv into the blob, I am not looking for other information.

import pandas as pd
import numpy as np
import datetime
import os, uuid
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
def function (df):
    df = df.rename(columns=df.iloc[1]).drop(df.index[0])
    df = df.iloc[1:]
    indexNames = df[df['Customer'].isin(['Stock', 'Sales', 'Over', '2021 Under'])].index
    df = df.drop(indexNames)
    df.columns = df.columns.fillna('ItemNo')
    for col in df:
        df['ItemNo'] = df['ItemNo'].ffill()
        
    
    return df
CONNECTION_STRING = ""
CONTAINERNAME = ""
BLOBNAME = ""
LOCALFILENAME = "" 

blob_service_client = BlobServiceClient.from_connection_string(CONNECTION_STRING) #instantiate new blobservice with connection string
#container_client = blob_service_client.get_container_client(CONTAINERNAME) #instantiate new containerclient
blob_client = blob_service_client.get_blob_client(container = CONTAINERNAME, blob=BLOBNAME)

#READ PRODUCTS FILE
f = open(LOCALFILENAME, "wb")
f.write(blob_client.download_blob().content_as_bytes())
f.close()
df = pd.read_excel(r''+LOCALFILENAME)

Solution

  • Maybe you can try the following code:

        temp_path = tempfile.gettempdir()
        file_path = os.path.join(temp_path, 'dataframe.csv')
        df.to_csv (file_path)
        with open(file_path, "rb") as data:
            blob_client.upload_blob(data)