[英]Python Pandas as pd set to_csv location
我已經基於另一個堆棧溢出后的腳本運行了這個腳本,並且我非常接近讓它執行我想要的操作。 最后一步是將新的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)
您可以設置確切的路徑。
如:
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.