I have a dataframes, I need to add 8 rows above the header of dataframe, I am sharing dataframe and the desired output
Dataframe:-
Toll No. Vr.name
245 2487 XR
145 3754 MY
55 7356 DF
65 4487 DF
28 7785 MY
47 8235 XR
87 2468 PQ
75 9735 GR
98 6486 TY
240 1023 PQ
rows need to be added
Car 8425
Booth No 24
Toll TR. Mr.XYZ
Date 05-JAN-22
Amt 123
Time 11:02:01
Mode QR
Tag YES
What Needed :-
- Want to add rows in dataframe above the header of dataframe.
- No. of rows may increase in some cases.
Desired Output
Car 8425
Booth No 24
Toll TR. Mr.XYZ
Date 05-JAN-22
Amt 123
Time 11:02:01
Mode QR
Tag YES
Toll No. Vr.name
245 2487 XR
145 3754 MY
55 7356 DF
65 4487 DF
28 7785 MY
47 8235 XR
87 2468 PQ
75 9735 GR
98 6486 TY
240 1023 PQ
CodePudding user response:
I made a separate datframe of rows, now I have two dataframes.
DF -
Car 8425
Booth No 24
Toll TR. Mr.XYZ
Date 05-JAN-22
Amt 123
Time 11:02:01
Mode QR
Tag YES
DF2:-
Toll No. Vr.name
245 2487 XR
145 3754 MY
55 7356 DF
65 4487 DF
28 7785 MY
47 8235 XR
87 2468 PQ
75 9735 GR
98 6486 TY
240 1023 PQ
Code to add both dataframe in CSV :-
with open((file_name), 'w',newline='') as f:
writer = csv.writer(f)
for index,row in df.iterrows():
writer.writerow(row)
writer.writerow(New_out)
for index1,row1 in df2.iterrows():
writer.writerow(row1)
I directly write both the dataframe in CSV file.
Outtput I got:-
Car 8425
Booth No 24
Toll TR. Mr.XYZ
Date 05-JAN-22
Amt 123
Time 11:02:01
Mode QR
Tag YES
Toll No. Vr.name
245 2487 XR
145 3754 MY
55 7356 DF
65 4487 DF
28 7785 MY
47 8235 XR
87 2468 PQ
75 9735 GR
98 6486 TY
240 1023 PQ