rdh9
rdh9

Reputation: 695

Pandas: how to write csv file using data from a dataframe as part of path/filename

Pandas: how to write csv file using data from a dataframe as part of path/filename

I have an example dataframe like this:

df = pd.DataFrame(np.random.randn(5), index=['a', 'b', 'c', 'd', 'e'])
print df

          0
a -0.899348
b  0.297170
c -0.998461
d  0.651564
e  2.013107

and a second dataframe with a single column containing a full path/filename

data2 = {'filepath': ['C:\\test\\testsub\\testfile.csv']}
path = pd.DataFrame(data2, columns=['filepath'])
print path

                       filepath
0  C:\test\testsub\testfile.csv

I can write the first dataframe to a csv like this:

df.to_csv('C:\\test\\testsub\\testfile.csv')

But I can't find a way to set the path/filename from the 'path' dataframe.

df.to_csv(path['filepath'])

...returns an error:

'TypeError: coercing to Unicode: need string or buffer, list found'

It seems the dataframe needs some form of conversion in order to be used here. I can't find any information on this. Can anybody enlighten me as to how I can make this work? Any and all help appreciated.

(In the bigger picture, the process is part of a loop where the required path/filename is returned in the dataframe 'path'. I can print the path and filename exactly as they should be, but I can't write the csv)

Upvotes: 0

Views: 4543

Answers (1)

Marius
Marius

Reputation: 60060

You just need to make sure that you're extracting a single element from the filepath column, rather that passing the whole column as the filename. You can do this with an indexing method like .ix[] or .iloc[]:

current_filepath = path['filepath'].iloc[0]

current_filepath
Out[8]: 'C:\\test\\testsub\\testfile.csv'

df.to_csv(current_filepath)

Because your example dataframe path just has integer indexes, you can also just do:

path['filepath'][0]

but this won't work the same way if you have a different index.

Upvotes: 2

Related Questions