-3

So i have a problem, that i need to get the cell's coordinates (in google-sheets) by it's content, for example a have a cell that has "hello" in it's self, and a have some amount of other cell's. For example "world" and i know that "hello" is in column 4 (there is only 1 hello in the example), how do i get it's coordinates on python 3, please help! here is how it lookes like in google-sheets : here is how it lookes like in google-sheets.

Please give me a code that will make this in my understanding:

if "hello" in sheet.col_values(4):

print(coordinates of "hello")

Kraego
  • 2,978
  • 2
  • 22
  • 34
54degree5
  • 12
  • 3

1 Answers1

0

How I approached your question:

  1. Since I have an excel sheet or csv file, I am going to "read" it via pandas and convert it to a pandas dataframe:
import pandas as pd
df = pd.read_csv('path/to/file', header=None) #if your file is .csv
df = pd.read_excel('path/to/file', header=None) #if your file is .xlsx
  1. Pandas Dataframe provide the iloc property: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html

Python lists are 0-based. So if I want an element that is located in my dataframe at 7th row /4th column, I will be able to get it like this:

df.iloc[6, 3]
  1. Use a for loop to iterate over the total number of dataframe rows:
for i in range(0, len(df)):
    if df.iloc[i, 3] == 'hello':
        print(i, 3)
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Thanks, but i ment the google-sheets, actually, i forgot to mention that( i cant convert it in pandas, becousei don't have a derictory to it, but, i think i will use the method of checking each column whith "for", thanks for the answer, but if u can, please make it for google sheets) – 54degree5 Sep 26 '21 at 07:48
  • So if I understood correctly you need a way to "read" this google sheet from google drive via Python. I assume you don't want to download the file and process it locally, because my previous answer would have covered you. Let's assume that you have a csv uploaded in your google drive. You can access it via google colab in the following way: 1. from google.colab import drive 2. drive.mount('/content/gdrive') – George Christodoulou Sep 26 '21 at 11:16
  • 3. use link and insert auth code 4. your current working directory (cwd) is "/content" 5. Now you can access your csv with pandas – George Christodoulou Sep 26 '21 at 11:23
  • I tried it, ind it works, but i have found (i spended 7 hours on finding it) an easier solution for python 3.9! Here it is! https://stackoverflow.com/questions/47831445/how-to-get-the-co-ordinate-of-a-cell-in-gspread-by-using-cell-value-in-python-3 – 54degree5 Sep 26 '21 at 15:16