0

I have the data in the form like

How do I separate or filter the rows based on the keywords of the genre(horror, thriller, etc) and store them for further processing(sorting)?enter image description here

piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • 2
    You should provide a [mcve] to make this as useful as possible. Also, I'm sure there is a duplicate question somewhere. – piRSquared Apr 29 '19 at 16:08
  • Possible duplicate of [How can I filter lines on load in Pandas read\_csv function?](https://stackoverflow.com/questions/13651117/how-can-i-filter-lines-on-load-in-pandas-read-csv-function) – Nicolò Gasparini Apr 29 '19 at 20:41

1 Answers1

1

You could maybe do:

f = open("myfile.csv", "r")
romance_mov = []

for line in f:

    if "romance" in line.split(",")[4].lower():
        romance_mov.append(line)
f.close()

Which would give you a list romance_mov with all the lines that are of genre 'romance'.

EDIT: For sorting the lines based on the value in hitFlop, you could then do:

import numpy as np

# Extract the hitFlop value for each row
hitFlop = []
for item in romance_mov:
    hitFlop.append(int(item.split(",")[-1]))

# Obtain the sorted indexes
idx_sorted = np.argsort(hitFlop)
# Sort the romance movies
romance_mov_sorted = np.asarray(romance_mov)[idx_sorted]
Walfits
  • 446
  • 5
  • 9