0

This is what my code looks like

pd.set_option('expand_frame_repr', False)
inventory_data = pd.read_csv('inventory_list_1.csv')
search_item = input('Enter the name of the item your looking for')
find_item = inventory_data['Item_Name'].astype(str).str.contains(serch_item)
print (find_item)
inventory_data.to_csv('inventory_list_1.csv', index=False, quoting=1)

This is an example of the CSV file with the top row being the header

[ITEM NAME 1], [QUANTITY 1], [PRICE 1]
pencil           3             4
pen              5             5

I want to be able to type in pen for the input

search_item = input('Enter the name of the item your looking for')

and get the following row back

pen              5             5

I'm kinda at a loss as how to do this

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101

3 Answers3

0

you could use the row function and iterrows

just like this

for index, row in df.iterrows():
    print(row['c1'], row['c2'])

Output: 
   10 100
   11 110
   12 120

source: How to iterate over rows in a DataFrame in Pandas

kalimdor18
  • 99
  • 13
0

Use this

for (row,rowSeries) in inventory_data.iterrows():
       print(row,rowSeries)
0

Here's a sample program to read the contents of csv file and check for an item.

inventory_list_1.csv

item,quantity,price
pencil,5,10
pen,5,5`

Sample Program

# import csv module
import csv
# Ask for user input
search_item = input('Enter the name of the item your looking for: ')
# read data from csv file
filePointer = open('inventory_list_1.csv')
input_file = csv.DictReader(filePointer)
# set a flag
found = False
# iterate through the contents and check for search_item
for row in input_file:
    if row['item'] == search_item:
       print(row['item'],row['quantity'],row['price'])
       found = True
       break
if not found:
    print(f"Item '{search_item}'' not found!")

# release
filePointer.close()
Bikash
  • 160
  • 1
  • 6