Python作为pd设置为_csv位置

Python作为pd设置为_csv位置,python,csv,pandas,Python,Csv,Pandas,我已经在另一个堆栈溢出帖子的基础上运行了这个脚本,我非常接近让它完成我想要它做的事情。最后一步是将新的csv保存在我添加为参数的第二个位置。在这段代码中,我想用destination替换“removed.csv”,但它不起作用。它保存在源代码所在的位置,我想告诉它保存在哪里。有人能帮我指出正确的方向吗?非常感谢 #!/usr/bin/python import sys import pandas as pd filename = sys.argv[1] destination = sys.

我已经在另一个堆栈溢出帖子的基础上运行了这个脚本,我非常接近让它完成我想要它做的事情。最后一步是将新的csv保存在我添加为参数的第二个位置。在这段代码中,我想用destination替换“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)

你只需要设定准确的路径

例如:

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)