在Pandas中将File_Path设置为to_csv() [英] Set File_Path for to_csv() in Pandas
本文介绍了在Pandas中将File_Path设置为to_csv()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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')
我想要一个变量,可以将to_csv中的路径设置为该变量.我试过path_or_buf = path.那也不行.
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.
推荐答案
您需要转义反斜杠或更好地使用原始字符串:
You need to either escape your back slashes or better use a raw string:
path='C:\\Users\\hvill\\Destop\\'
或更少的字符更好:
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'))
这会将您的csv名称正确地附加到目标路径
this will append your csv name to your destination path correctly
这篇关于在Pandas中将File_Path设置为to_csv()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文