pandas 添加pandas to_csv 函数的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52521594/
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
Adding a path to pandas to_csv function
提问by Warthog1
I have a small chunk of code using Pandas that reads an incoming CSV, performs some simple computations, adds a column, and then turns the dataframe into a CSV using to_csv.
我有一小段代码使用 Pandas 读取传入的 CSV,执行一些简单的计算,添加一列,然后使用 to_csv 将数据帧转换为 CSV。
I was running it all in a Jupyter notebook and it worked great, the output csv file would be there right in the directory when I ran it. I have now changed my code to be run from the command line, and when I run it, I don't see the output CSV files anywhere. The way that I did this was saving the file as a .py, saving it into a folder right on my desktop, and putting the incoming csv in the same folder.
我在 Jupyter 笔记本中运行它,它运行良好,当我运行它时,输出的 csv 文件就在目录中。我现在已将代码更改为从命令行运行,当我运行它时,我在任何地方都看不到输出 CSV 文件。我这样做的方法是将文件另存为 .py,将其保存到我桌面上的一个文件夹中,然后将传入的 csv 放在同一个文件夹中。
From similar questions on stackoverflow I am gathering that right before I use to_csv at the end of my code I might need to add the path into that line as a variable, such as this.
从stackoverflow上的类似问题中,我在代码末尾使用to_csv之前收集了这些信息,我可能需要将路径作为变量添加到该行中,例如this。
path = 'C:\Users\ab\Desktop\conversion'
final2.to_csv(path, 'Combined Book.csv', index=False)
However after adding this, I am still not seeing this output CSV file in the directory anywhere after running my pretty simple .py code from the command line.
但是,在添加了这个之后,在从命令行运行我非常简单的 .py 代码后,我仍然没有在目录中的任何地方看到这个输出 CSV 文件。
Does anyone have any guidance? Let me know what other information I could add for clarity. I don't think sample code of the pandas computations is necessary, it is as simple as adding a column with data based on one of my incoming columns.
有没有人有任何指导?让我知道我可以添加哪些其他信息以便清楚。我不认为 Pandas 计算的示例代码是必要的,它就像添加一个包含基于我传入的列之一的数据的列一样简单。
回答by Karn Kumar
I think below is what you are looking for , absolute path
我认为下面是你要找的,绝对路径
import pandas as pd
.....
final2.to_csv('C:\Users\ab\Desktop\conversion\Combined Book.csv', index=False)
OR for an example:
或例如:
path_to_file = "C:\Users\ab\Desktop\conversion\Combined Book.csv"
final2.to_csv(path_to_file, encoding="utf-8")
回答by T Burgis
Join the path and the filename together and pass that to pd.to_csv
:
将路径和文件名连接在一起并将其传递给pd.to_csv
:
import os
path = 'C:\Users\ab\Desktop\conversion'
output_file = os.path.join(path,'Combined Book.csv')
final2.to_csv(output_file, index=False)
回答by fhi
Im pretty sure that you have mixed up the arguments, as shown here. The path should include the filename in it.
Im相当肯定你已经混了参数,如这里。路径中应包含文件名。
path = 'C:\Users\ab\Desktop\conversion\Combined_Book.csv'
final2.to_csv(path, index=False)
Otherwise you are trying to overwrite the whole folder 'conversions' and add a complicated value separator.
否则,您将尝试覆盖整个文件夹“conversions”并添加复杂的值分隔符。