Python Pandas as pd设置为to_csv位置

时间:2016-06-22 20:21:18

标签: python csv pandas

我已经让这个脚本基于另一个堆栈溢出帖子工作了,而且我已经接近让它做我想做的事了。最后一步是将新的csv保存在我添加的第二个位置作为参数。在此代码中,我想替换" removed.csv"与目的地,但它不起作用。它将它保存在源代码所在的位置,我想告诉它保存的位置。谁能帮助我指出正确的方向?非常感谢!

#!/usr/bin/python

import sys

import pandas as pd

filename = sys.argv[1]
destination = sys.argv[2]

df = pd.read_csv(filename)

keep_cols = ["Path", "IPTC - Title", "Description", "Person(s) of Import", "Museum Location", "Catalog Name", "Catalog Number", "Catalog Link", "Keywords", "Special Exhibitions", "Donor Credit", "Video Licensing", "Image Licensing", "Music Licensing", "Audio Licensing", "Relate Assets", "IPTC - Creator", "IPTC - City", "IPTC - Country", "Rights Usage Terms"]

new_df = df[keep_cols]

new_df.to_csv("removed.csv", index=False)

1 个答案:

答案 0 :(得分:1)

您可以设置确切的路径。

如:

new_df.to_csv(r"C:\users\mthiesen\desktop\python\removed.csv", index=False)

或类似的东西:

path_to_output = r'C:\Users\clickhere\Desktop'
new_df.to_csv(path_to_output + r'\output.csv')

注意:您还可以通过仅接收需要la:

的列来提高性能
keep_cols = ["Path", "IPTC - Title", "Description", "Person(s) of Import", "Museum Location", "Catalog Name", "Catalog Number", "Catalog Link", "Keywords", "Special Exhibitions", "Donor Credit", "Video Licensing", "Image Licensing", "Music Licensing", "Audio Licensing", "Relate Assets", "IPTC - Creator", "IPTC - City", "IPTC - Country", "Rights Usage Terms"]
new_df = pd.read_csv(filename,usecols=keep_cols)