2
People       OS      Games Owned
Anthony   Windows       120
Alissa    Windows       70
Jordan    Windows       20
Khan        Mac         47
Benny       Mac         23
Anastasia  Linux        16
McCurdy     Linux       10

I was wondering, How would I go about filtering out people who own over 20 games, and they don't have a Mac OS System. I need it to be done via a python script, and when run, it outputs its data in a seperate file, like a text file or something. Thanks!

0

2 Answers 2

7

I'd suggest using the Pandas library.

Code is basically as follows:

import pandas as pd

data = pd.read_csv('put in your csv filename here')
# Filter the data accordingly.
data = data[data['Games Owned'] > 20]
data = data[data['OS'] == 'Mac']
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help!
And just to complete the answer for @SkytechCEO - write the filtered data out to another csv with: data.to_csv('output.csv')
3

Here's a solution in pure python that writes the filtered output to a textfile (csv) as requested.

import csv

with open('games.csv', 'rb') as csvfile:
    # handle header line, save it for writing to output file
    header = next(csvfile).strip("\n").split(",")
    reader = csv.reader(csvfile)
    results = filter(lambda row: row[1] != 'Mac' and int(row[2]) > 20, reader)

with open('output.csv', 'wb') as outfile:
    writer = csv.writer(outfile)
    writer.writerow(header)
    for result in results:
        writer.writerow(result)

Comments

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.