Set File_Path for to_csv() in Pandas

Jase Villam picture Jase Villam · Apr 4, 2014 · Viewed 64.9k times · Source
funded=r'C:\Users\hill\Desktop\wheels\Leads(1).csv'
funded= read_csv(funded)
funded=DataFrame(funded)
path='C:\Users\hvill\Destop\ '
funded.to_csv(path,'greenl.csv')

I want to have a variable that I can set the path in to_csv to. I tried path_or_buf = path. That doesn't work either.

Answer

EdChum picture EdChum · Apr 4, 2014

You need to either escape your back slashes or better use a raw string:

path='C:\\Users\\hvill\\Destop\\'

or better as fewer characters:

path=r'C:\Users\hvill\Destop\'

I also think you want to do this when saving:

funded.to_csv(path+'greenl.csv')

To avoid the ambiguity and allow portability of your code you can use this:

import os
funded.to_csv(os.path.join(path,r'green1.csv'))

this will append your csv name to your destination path correctly