Home > Net >  How can I filter keywords from a csv file using Python
How can I filter keywords from a csv file using Python

Time:02-22

I am trying to write a Python code which opens a csv file, filter keywords from specific columns and display the results.

The csv file has 500 rows and 7 columns separated by comma.

That's my code. Not sure if makes much sense.

with open('csv_file') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        Title == row[4]
        Author(s) == row[5]
        Genre == row [7]
        if row[1] == entrada:
            results.append()

    print(Results.format(entrada, results))

CodePudding user response:

This should work for single column

import pandas as pd

df = pd.read_csv('path/to/file.csv')

print(df.loc[df['Author(s)'].str.contains(keyword)]])

you can search for multiple keywords like this :

keyword="Mat|Tom"

  • Related