pythonpygsheets

In pygsheets what is the proper way of selecting negative numbers?


I am using pygsheets to make a budget. I want to be able to store all the negative cells in some sort of dictionary (I'm not great with python yet)

I've been able to select a DataRange of cells, but how do I add a filter to that?

For example, drange = pygsheets.DataRange(start='A1', worksheet=wks) this is one of my ranges. How would I add a filter to this to only select negative numbers?


Solution

  • This is a simple solution.

    import pygsheets
    
    client = pygsheets.authorize(service_file="cred.json", local=True)
    sh = client.open('Testing Excel')
    wks = sh.sheet1
    #This will drag the cell data from range A1:A10 and transform all the string to float
    date_unfiltered =[float(*i) for i in wks.get_values(start = "A1", end = "A10")]
    #This will filter out all the negative values and return it as a list
    data_filtered = list(filter(lambda money: money < 0, date_unfiltered))
    print(data_filtered)