Sean
Sean

Reputation: 3450

Is there a more efficient way to store Pandas DataFrames to CSV?

I've written a small Python script that performs operations on a CSV file and stores the newly modified one. I'm wondering if there are any functions or modules that I could take advantage of to make it more efficient. Here's the script:

import pandas as pd
import os


print("Current directory is:\n" + os.getcwd() + '\n')
csv = input("Please enter csv file name: ")
csv_list = csv.split('/')

df = pd.read_csv(csv)
df.drop(df[df['is_reply_to'] == 1].index, inplace=True)
df.to_csv('./' + csv_list[-2] + '/' + 'new_' + csv_list[-1])

Example input: ./upper_directory/testing.csv

Example ouput: new_testing.csv


The method that I'm using is very specific in the sense that I'm assuming that the target CSV file is located in a directory inside the current directory. I was wondering if there was any way to make it more general in the sense that I don't have to do things like csv_list[-2] + '/' + ....

Thank you!

Upvotes: 0

Views: 156

Answers (1)

ErikXIII
ErikXIII

Reputation: 557

You can create better looking paths like this:

import os
# Directory path of input, then actual file name of path.
out_path = os.path.join(os.path.dirname(csv), 'new_{}'.format(os.path.basename(csv)))
df.to_csv(out_path)

Upvotes: 2

Related Questions