3

I currently have a task that involves downloading a CSV master file, removing any lines where column A - Column B <= 0, and where Column C equals a given phrase. I'm looking to a create a program that will:

  • Import a CSV File
  • Remove all lines where Column A - Column B <= 0
  • Ask for input to filter on Column C for one or more phrases
  • Export the CSV into a new file

So far, I have determined that the best way to do this is to use Pandas' dataframe functionality, as I've used it previously to perform other operations on CSV files:

import pandas as pd

file = read_csv("sourcefile.csv")
file['NewColumn'] = file['A'] - file['B']
file = file[file.NewColumn > 0]
columns = ['ColumnsIWantToRemove']
file.drop(columns, inplace=True, axis=1)
phrases = input('What phrases are you filtering for? ')
file = file[file.C = phrases]
file.to_csv('export.csv')

My question is, how do I filter Column C for multiple phrases? I want the program to take one or more phrases and only show rows where Column C's value equals one of those values. Any guidance would be amazing. Thank you!!

2 Answers 2

1

I would just ask for input to be comma separated:

phrases = phrases.split(",")
file = file[file.C.isin(phrases)]
Sign up to request clarification or add additional context in comments.

2 Comments

I've tried this, but it doesn't seem to be filtering and when I export the data file it comes up blank. Any ideas?
@dner print statements are you friend here, you should try printing phrases before and after the split, and then you can see if you can replicate this in the repl (e.g. ipython).
0

maybe this can help you :

import csv

input = open(sourcefile.csv, 'rb')
output = open(out_sourcefile, 'wb')
writer = csv.writer(output)
for row in csv.reader(input):
    if (phrases you want C column not to be,and you can add here multiple phrases):
        continue
        writer.writerow(row)
input.close()
output.close()

1 Comment

This doesn't seem to work because we have hundreds of phrases we don't want to keep

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.