How to save a pandas dataframe as a csv file in Azure Blob Storage from Azure ML Notebook

Sirivella Zephaniah 60 Reputation points
2024-01-19T10:44:49.6+00:00

What is the process to save a pandas dataframe as a csv file in Azure Blob Storage from Azure ML Notebook? I have the dataframe in the notebook and would like to store it as a csv file in Blob Storage.

Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
3,332 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,192 questions
0 comments No comments
{count} votes

Accepted answer
  1. santoshkc 15,245 Reputation points Microsoft External Staff Moderator
    2024-01-19T11:09:33.8+00:00

    Hi @Sirivella Zephaniah,

    Thank you for reaching out to Microsoft Q&A forum! To save a pandas dataframe as a CSV file in Azure Blob Storage from Azure ML Notebook, you can use the azure-storage-blob library. I tried to repro with the below code snippet for your reference:

    from azure.storage.blob import BlobServiceClient
    import pandas as pd
    import io
    
    # Define your Azure Blob Storage account details
    account_name = '<your_account_name>'
    account_key = '<your_account_key>'
    container_name = '<your_container_name>'
    blob_name = '<your_blob_name>'
    
    # Create a BlobServiceClient object
    blob_service_client = BlobServiceClient(account_url=f"https://{account_name}.blob.core.windows.net", credential=account_key)
    
    # Convert your pandas dataframe to a CSV string
    df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})
    csv_string = df.to_csv(index=False)
    
    # Convert the CSV string to a bytes object
    csv_bytes = str.encode(csv_string)
    
    # Upload the bytes object to Azure Blob Storage
    blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)
    blob_client.upload_blob(csv_bytes, overwrite=True)
    

    Please provide the Blob Storage account details in the above code and execute. In Blob storage you can find the CSV file as below:

    enter image description here


    If this answers your query, do click Accept Answer and Yes for was this answer helpful.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.