Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

swap column using dataframe in csv file- Python

I want to swap columns from csv file using dataframe in s3 bucket. Dataframe has header row which has column name(sa_instance_state, data_interval_start,NAME)

My piece of code:

def modify_csv(**kwargs):
    s3=boto3.client('s3')
    read_file = s3.get_object(Bucket=Bucket, Key=Key)
    df = pd.read_csv(read_file['Body'],sep=',')
    print(df)
    

Input dataframe (print(df))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

sa_instance_state     data_interval_start  NAME 
<sqlalchemy.orm.state.InstanceState1> 2022-07-17 12:55:00+00:00  XYZ
<sqlalchemy.orm.state.InstanceState2> 2022-07-17 12:58:00+00:00  abx

Expected Result:

NAME  sa_instance_state     data_interval_start   
XYZ   <sqlalchemy.orm.state.InstanceState1> 2022-07-17 12:55:00+00:00  
abx   <sqlalchemy.orm.state.InstanceState2> 2022-07-17 12:58:00+00:00

>Solution :

Maybe this is what you are looking for?:

df = df[['NAME', 'sa_instance_state', 'data_interval_start']]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading