Python Pandas as pd设置为\u csv位置

2024-06-16 10:21:27 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: csvimport脚本dfnew堆栈sysfilename
1条回答
网友
1楼 · 发布于 2024-06-16 10:21:27

你可以设定确切的路径。在

如:

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

或者像这样:

^{pr2}$

注意:您还可以通过只在需要la的列中输入来提高性能:

^{3}$

相关问题 更多 >