Maile Thiesen
Maile Thiesen

Reputation: 131

Python Pandas as pd set to_csv location

I've got this script working based on another stack overflow post and I'm so close to having it do what I want it to do. The last step is getting the new csv to save in the second location I add as an argument. In this code i'd like to replace "removed.csv" with destination, but it's not working. It saves it where the source code lives, and I want to tell it where to save. Can anyone help point me in the right direction? Thanks so much!

#!/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)

Upvotes: 1

Views: 3186

Answers (1)

click here
click here

Reputation: 836

You could just set the exact path.

As in:

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

or something like this:

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

Note: You could also improve performance by only taking in the columns that you need a 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)

Upvotes: 2

Related Questions