在 Pandas 中为 to_csv() 设置 File_Path

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22872952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:13:26  来源:igfitidea点击:

Set File_Path for to_csv() in Pandas

pandasexport-to-csv

提问by Jase Villam

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.

我想要一个变量,我可以将 to_csv 中的路径设置为。我试过 path_or_buf = 路径。那也行不通。

回答by EdChum

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

这会将您的 csv 名称正确附加到您的目标路径