Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
635 views
in Technique[技术] by (71.8m points)

python - Export pandas data frame to Azure Data Lake Storage as a CSV file?

This may be an uncommon question as I believe it has never been asked before, but is it possible to export a Pandas data frame straight to an Azure Data Lake Storage as a CSV file?

To add some context, I have a pandas dataframe which gets exported as a CSV file to a local directory, using the datalakeserviceclient I then get the CSV file from the file path and write the file into the data lake storage.


docs[:0].to_csv("test.csv", index = False)
docs.to_csv("test.csv", index = False, header = False ,mode = 'a', quoting = csv.QUOTE_NONNUMERIC)

try:  
    global service_client
        
    service_client = DataLakeServiceClient(account_url="{}://{}.dfs.core.windows.net".format(
        "https", "XXXX"), credential='XXX')
    

    file_system_client = service_client.get_file_system_client(file_system="root")

    directory_client = file_system_client.get_directory_client("test_db") 

    file_client = directory_client.create_file("test.csv") 
    local_file = open(r"C:XXXXest.csv",'rb') 

    file_contents = local_file.read()

    file_client.upload_data(file_contents, overwrite=True) 


except Exception as e:
    print(e) 


However, I don't want the data frame to be exported to my local directory, instead I want to find a way to export it straight to the data lake storage. Is this actually possible?

Any help is appreciated

question from:https://stackoverflow.com/questions/66060675/export-pandas-data-frame-to-azure-data-lake-storage-as-a-csv-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

pandas.to_csv (doc) can save the dataframe into a buffer.

Try the following code:

from io import StringIO
text_stream = StringIO()

docs.to_csv(text_stream)
# the rest of your code

file_client.upload_data(text_stream, overwrite=True)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...