Home > Software engineering >  How to drop the index after creating the csv file in pandas
How to drop the index after creating the csv file in pandas

Time:03-20

I am trying to select couple of columns based on column heading with wild card and one more column. When I execute the below code , I am getting the expected result, but there is an index which is appearing. how to drop the index . Any suggestions.

infile:

dir,name,ct1,cn1,ct2,cn2
991,name1,em,[email protected],ep,1234
999,name2,em,[email protected],ep,12345
872,name3,em,[email protected],ep,123456

here is the code which I used.

import pandas as pd
df=pd.read_csv('infile.csv')
df_new=df.loc[:,df.columns.str.startswith('c')]
df_new_1=pd.read_csv('name.csv', usecols= ['dir'])
df_merge=pd.concat([df_new,df_new_1],axis=1, join="inner")
df_merge.to_csv('outfile.csv')

CodePudding user response:

Pass false for index when you save to csv :

df_merge.to_csv('outfile.csv', index=False)
  • Related