0

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))
0

1 Answer 1

1

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"

Sign up to request clarification or add additional context in comments.

5 Comments

That worked perfectly, because I create a new file with a single column. Thank you very much! I appreciate your help!
What if the user wants to search for the ISBN number of a book. In the csv file there is a column for ISBN number of each book.
@BecKy wouldnt changing the the column name to 'ISBN' helps? for exact match this also works: df[df['IBSN'] == 'ibsn_to_search'].
it means there is no column with that name you can get a sense of your dataframe by print(df.head()), also list columns by print(list(df.columns))
I modified the original answer. you can use .loc

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.