Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Unable to remove Rows from existing csv file in AWS S3 using python, Currently, I was able to read write and append data using my code, but when i try to drop a row, i got the following error

MY CODE

Python
import pandas as pd
import boto3
import csv
Name = "database-2"
Launch_time = "2021-07-09T11:04:80z"
Region = "us-east-2"
data_list = {'Instance_name': [Name],
            'Created_time ':  [Launch_time],
            'Region       ':  [Region]}
s3_client = boto3.client('s3')
df = pd.DataFrame(data_list)
bytes_to_write = df.to_csv(None, header=None, index=False).encode()
print(bytes_to_write)
file_name = 'blank.csv'
# get the existing file
current_data = s3_client.get_object(Bucket='test-lambda', Key=file_name)['Body'].read()
# append
print(current_data)
appended_data = current_data + bytes_to_write
# drop raw 2
appended_data = appended_data.drop([2], axis=0)
# overwrite
s3_client.put_object(Body=appended_data, Bucket='test-lambda', Key=file_name)


ERROR MESSAGE

Python
Traceback (most recent call last):
  File "script.py", line 21, in <module>
    appended_data = appended_data.drop([2], axis=0)
AttributeError: 'bytes' object has no attribute 'drop'


how to resolve this? please help me...

What I have tried:

I have tried with a csv file in the local machine then the .drop option is worked
Posted
Comments
Richard MacCutchan 13-Jul-21 3:56am    
You cannot call that method on a byte array.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900