Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import boto3
- from StringIO import StringIO
- def write_df_to_s3(df, s3_bucket, s3_key, encoding='utf-8', index=False, **kwargs):
- """
- Writes a pandas dataframe to a csv on AWS S3
- Arguments:
- df (pd.DataFrame): dataframe to write
- s3_bucket (str): s3 bucket name, e.g. "my-bucket"
- s3_key (str): s3 bucket key to write file as, e.g. "data/myfile.csv"
- kwargs: arguments to pass to pd.DataFrame.to_csv()
- """
- csv_buffer = StringIO()
- df.to_csv(csv_buffer, encoding=encoding, index=index, **kwargs)
- s3_resource = boto3.resource('s3')
- s3_resource.Object(s3_bucket, s3_key).put(Body=csv_buffer.getvalue())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement