0

iam trying to add a totally different row before the data frame and i want to ignore the already existing header.

df1

         A    B    C   D   E   F      G      H
0  ABC1234  123  abc  45  64  34  Aruna  67044
1  DEF2344  345  red  34  43  56  Jason  83234

df2

'HEADER|'+ str(ODATE) + '|' + (str(ODATE-datetime.timedelta(days=1)))

But It is coming like this:

HEADER|20221008|20221007||||||||
ABC1234|123|abc|45|64|34|Aruna|67044
DEF2344|345|red|34|43|56|Jason|83234

I want output like this:

HEADER|20221008|20221007
ABC1234|123|abc|45|64|34|Aruna|67044
DEF2344|345|red|34|43|56|Jason|83234

I am using below code to get the date and concatanate both dataframes

const  = 'HEADER|'+ str(ODATE) + '|' + (str(ODATE-datetime.timedelta(days=1)))
df2  = pd.Series([const])
df3  = pd.concat([df2,df1],ignore_index=True)
df3.to_csv(final_output_file, index=False,sep='|',header=False)

How to get only like this at top HEADER|20221008|20221007

BeRT2me
  • 12,699
  • 2
  • 13
  • 31
  • Would it be good enough to write a custom header to the CSV file, then write the data? Example: https://stackoverflow.com/questions/56166681/how-to-write-a-pandas-dataframe-to-csv-file-with-custom-header – Nick ODell Oct 09 '22 at 00:00
  • Thanks .I did that but the header is coming at the end. How to make the const value come at the top? with open(final_output_file,'a') as file: file.write(const) df2.to_csv(final_output_file,header=False,sep='|',index=False) – Lakshmi Sudarsanam Oct 09 '22 at 20:49

0 Answers0