Home > Net >  Deleting an unnamed column from a csv file Pandas Python
Deleting an unnamed column from a csv file Pandas Python

Time:11-07

I ma trying to write a code that deletes the unnamed column , that comes right before Unix Timestamp. After deleting I will save the modified dataframe into data.csv. How would I be able to get the Expected Output below?

import pandas ads pd 

data = pd.read_csv('data.csv')
data.drop('')
data.to_csv('data.csv')

data.csv file

,Unix Timestamp,Date,Symbol,Open,High,Low,Close,Volume
0,1635686220,2021-10-31 13:17:00,BTCUSD,60638.0,60640.0,60636.0,60638.0,0.4357009185659157
1,1635686160,2021-10-31 13:16:00,BTCUSD,60568.0,60640.0,60568.0,60638.0,3.9771881707839967
2,1635686100,2021-10-31 13:15:00,BTCUSD,60620.0,60633.0,60565.0,60568.0,1.3977284440628714

Updated csv (Expected Output):

Unix Timestamp,Date,Symbol,Open,High,Low,Close,Volume
1635686220,2021-10-31 13:17:00,BTCUSD,60638.0,60640.0,60636.0,60638.0,0.4357009185659157
1635686160,2021-10-31 13:16:00,BTCUSD,60568.0,60640.0,60568.0,60638.0,3.9771881707839967
1635686100,2021-10-31 13:15:00,BTCUSD,60620.0,60633.0,60565.0,60568.0,1.3977284440628714

enter image description here

CodePudding user response:

This is the index. Use index=False in to_csv.

data.to_csv('data.csv', index=False)

CodePudding user response:

Set the first column as index df = pd.read_csv('data.csv', index_col=0) and set index=False when writing the results.

CodePudding user response:

you can follow below code.it will take column from 1st position and then you can save that df to csv without index values.

df = df.iloc[:,1:]
df.to_csv("data.csv",index=False)
  • Related