-1

Assume this is my csv file: (df)

id,name,version,ct_id
1,testing,version1,245
2,testing1,version2,246
3,testing2,version3,247
4,testing3,version4,248
5,testing1,version5,249

Now I've performed some operation on the file and write it to another csv file.

df = pd.read_csv('op.csv')

df1 = df.groupby('name').agg({'version': ', '.join, 'ct_id': 'first'}).reset_index()

df1.to_csv('test.csv', index=False) 

Now I've another csv file. (df_1)

id,name,version,ct_id
36,testing17,version16,338
37,testing18,version17,339

I want to write this to my existing test.csv file which I created earlier but I want to insert these two rows at the beginning of the file rather than at the end.

I tried something like this.

df_1.iloc[:, 1:].to_csv('test.csv', mode='a', index=False) 
# This does append but at the end.

I would appreciate if someone could help?

feedMe
  • 3,431
  • 2
  • 36
  • 61
Goutham
  • 435
  • 3
  • 16
  • 1
    Side note: "append" specifically means "to the end of". You want to insert. – roganjosh Nov 02 '18 at 14:21
  • 2
    It seems you can't do that. Read the answers [here](https://stackoverflow.com/questions/125703/how-to-modify-a-text-file) – Sheldore Nov 02 '18 at 14:22
  • @Bazingaa I think that is too specific. You could concat the two sets of data and overwrite the existing file. For all intents and purposes, it's still the same old file but modified – roganjosh Nov 02 '18 at 14:24
  • 1
    Append means add at the end. You are asking how to *insert* data at the start of a file. The only way to do that is to move the existing data to make space for the new one. If anything goes wrong while moving the data, you end up with garbage. A *safer* option is to copy the new and old data to a *new* file and replace the original. Another option is to load the dataframe again, add the new lines then write it out over the original file. – Panagiotis Kanavos Nov 02 '18 at 14:24
  • Why do you want to insert the rows anyway? Is there any particular reason, eg there's a lot of data (100s of MBs) which makes loading the CSV expensive? – Panagiotis Kanavos Nov 02 '18 at 14:27
  • @PanagiotisKanavos Yes I'm reading the file in chunks and making modifications and want to prepend those rows at the beginning. I've looked all over S/O couldn't find anything that could help me. So I asked to see if anyone can help. – Goutham Nov 02 '18 at 14:31

1 Answers1

3

Prepending A in B is same as appending B to A. The below code should work for the above case.

test_df = pd.read_csv('test.csv')
df_1 = pd.read_csv('df_1.csv')

df_1 = df_1.append(test_df, sort=False)
df_1.to_csv('test.csv')
Mahendra Singh
  • 508
  • 2
  • 9