Search code examples
pythonpandas

pandas: get scalar value by column


I have a dataframe and I want get a single scalar value from column store_id which contains same values for all rows.

df = pd.DataFrame(
    {
        "id": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
        "contents": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
        "store_id": [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
    }
)

store_id = int(df["store_id"].max())

The above is giving me result, just wondering will it be slow if the dataframe is very big. As I am retrieving the dataframe from some other function, say for eg it may returns a dataframe that may contain 10 or 300 or big number of rows(i.e. dynamic) and all will have store_id column.

I also tried

store_id = df["store_id"].squeeze() # but it did not worked

Is there more effecient way to achieve the same?


Solution

  • Don't apply any computation, since all values are the same just get the first one:

    store_id = df['store_id'].iloc[0]
    

    Or:

    store_id = df.iloc[0, df.columns.get_loc('store_id')]
    

    Or:

    store_id = df.loc[df.first_valid_index(), 'store_id']