'Saving Pandas dataframe to SharePoint location as csv file

I want to save my transformed dataset to csv on SharePoint location.

Can anyone help with how to approach. I have gone through Libraries shareplum but they read data from local and then upload to SharePoint.

I want to save pandas df directly to SharePoint as csv or excel file .



Solution 1:[1]

I have done similar data transfer to load data from snowflake table to files in SharePoint using dataframe. To load the files to Sharepoint I'm using Shareplum package. The below code takes data from Snowflake table and writes to a dataframe, this is then processed as files to SharePoint folder.

from shareplum import Office365
from shareplum import Site
from shareplum.site import Version
import pandas as pd

#connecting to SharePoint folder
authcookie = Office365(server_url, username = Username,password=Password).GetCookies()
site = Site(site_url, version=Version.v365, authcookie=authcookie)
folder = site.Folder(Sharepoint_folder)

#creating a dataframe 'df' from sql
sql_query = '''SELECT * FROM "DATABASE"."SCHEMA"."TABLE" '''
df= pd.read_sql(sql_query,con_var) # con_var is a variable holding snowflake connection details

#uploading file from dataframe to SharePoint folder
filename = 'my_file_name.csv'
folder.upload_file(df.to_csv(filename, encoding='utf-8'))

Let me know if this worked for you or else we can try for other alternatives

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1