Created
June 25, 2017 22:31
-
-
Save kunanit/5c12fbc97e3e961f4f988c9d2d1a7221 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment