Setting a variable into a iloc function

I would like to retrieve data from a dataframe with the iloc function. For ex. df.iloc[5:,1] but i want to set the line number to reach with an external variable. I tried to set from_val = '5:' and df.iloc[from_val,1] but it's not working. I've a error of : "ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types"

How to perform ? Best regards

1 answer

  • answered 2020-01-21 22:02 Grzegorz Skibinski

    Try:

    from_val = 5
     df.iloc[from_val:,1] 
    

    Alternatively:

    from_val="5:"
    eval(f"df.iloc[{from_val}, 1]")