0

I have a xlsx file with game names, their prices and the links to their sites where we can find them. I'm trying to make a program where someone can type one word and the program will search for every game with this word in its name and show its price and link.

      nome                  preco    link
0   Fifa 20              R$ 164,90  
1   FIFA 19              R$ 84,90    https://www.americanas.com.br/produto/13379718...
2   EFootball PES 2020   R$ 93,88    https://www.americanas.com.br/produto/13456974...
3   Forza Horizon 4      R$ 199,90   https://www.americanas.com.br/produto/13379732...
4   Mortal Kombat 11     R$ 129,90   https://www.americanas.com.br/produto/13416378...

Since I'm a beginner I don't know how to start, even though I already tried regex. Could someone help me? Thank you

François B.
  • 1,096
  • 7
  • 19

2 Answers2

1

Start with loading the whole thing into a pandas dataframe.

import pandas as pd
df = pd.read_excel('my_file.xlsx')

Then assuming you don't need a fancy UI:

nm = input('Enter a partial name of a game: ')
print(df[df['name'].str.contains(nm)])

Now that just prints the segment of the dataframe. If you wanted to grab a particular cell:

link = df.loc[df['name'].str.contains(nm), 'link']
print(link)

If you want to see all the games which include nm but only certain columns, you can either do this... (include to_string() to make sure whole dataframe displays)

disp_cols = ['nome', 'preco', 'link']
print(df.loc[df['name'].str.contains(nm), disp_cols].to_string())

Or you can loop through them similar to @b-bogart solution. I also second that using .lower() is usually a good idea to make life easier on both the user and to avoid troublesome data cleanup.

Tom
  • 1,003
  • 2
  • 13
  • 25
0

I would use pandas to import and search, then loop through the results and print your output.

import pandas as pd
df = pd.read_excel('64653127.xlsx')

search = 'fifa'

#use str.lower() for case insensitive results
results = df[df['nome'].str.lower().str.contains(search)]
#print(results)
for index, row in results.iterrows():
    print(str(row['nome']) + ' link: ' + str(row['link']))

The output is:

Fifa 20 link: nan
FIFA 19 link: https://www.americanas.com.br/produto/13379718...
B. Bogart
  • 998
  • 6
  • 15