Formatting a csv file by deleteing and trasnforming columns with Pandas Python

I want to modify a data frame in python with the pandas function. So I want to delete the first and the 7th column (Unix Timestamp, Close). I have also moved the column Symbol to the end of the columns. How could I do these transformations to the csv file below. I want the formats to be written in the csv file permanently, would this be possible?

import pandas as pd
url= `input.csv`
data = pd.read_csv(url, low_memory=False)

original csv:

enter image description here

Formatted csv/Expected Output: enter image description here

Answer

Firstly drop your columns by drop() method:-

data=data.drop(columns=['Unix Timestamp','Close'])

Now use pop() method:-

symbol=data.pop('Symbol')

Finally:-

data['Symbol']=symbol

After that if you wants to change the contents of csv file then save this data to your csv file by to_csv() method

data.to_csv(url)

Note:- If you don’t want to save index then pass index=False in to_csv() method

Leave a Reply

Your email address will not be published. Required fields are marked *