Home > OS >  swap column using dataframe in csv file- Python
swap column using dataframe in csv file- Python

Time:07-20

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))

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

CodePudding user response:

You can use set_index and reset_index:

>>> df.set_index('NAME').reset_index()
  NAME                      sa_instance_state        data_interval_start
0  XYZ  <sqlalchemy.orm.state.InstanceState1>  2022-07-17 12:55:00 00:00
1  abx  <sqlalchemy.orm.state.InstanceState2>  2022-07-17 12:58:00 00:00

CodePudding user response:

Maybe this is what you are looking for?:

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

  • Related