0

I was just experimenting with how to read excel sheets using pandas. I know how to sort out rows and columns using serial numbers. The general way to read the excel spreadsheet is:

file = ('path') 
new = pds.read_excel(file) 
new

Also using the usecols parameter I can print specific columns. But, in the program I was testing out, I needed a string based input. For example, if in the Excel spreadsheet I have a row named 'Food', then when I input the string Food, I should get the corresponding row. But apparently pandas only works using serial numbers.

I wanted to know whether there is a way in which I can browse through and print specific excel rows using only string based inputs?

PNS
  • 101
  • 2
  • 1
    do you mean you want to search the entire df for this row? if so `df.loc[df.eq('Food').any(axis=1)]` should work. – Umar.H Sep 20 '20 at 05:25
  • 1
    Does this answer your question? [pandas loc vs. iloc vs. ix vs. at vs. iat?](https://stackoverflow.com/questions/28757389/pandas-loc-vs-iloc-vs-ix-vs-at-vs-iat) – Trenton McKinney Sep 20 '20 at 05:31
  • 1
    Does this answer your question? [How are iloc and loc different?](https://stackoverflow.com/questions/31593201) – Trenton McKinney Sep 20 '20 at 05:32
  • 1
    You can load the sheet to pandas with any kind of index you want (numerical or categorical). For example you can create an index with whatever words you like and select the rows with `df.loc['Food']` – IoaTzimas Sep 20 '20 at 06:59

1 Answers1

0

If everything is in a dataframe you use:

df.loc["rowname"]

so in your case if the dataframe is named new:

new.loc["rowname"]

if you want all rows in a list it would be:

new.index.to_list()
PBjarterot
  • 47
  • 6