Home > Enterprise >  i need to return a value from a dataframe cell as a variable not a series
i need to return a value from a dataframe cell as a variable not a series

Time:05-02

i have the following issue:

when i use .loc funtion it returns a series not a single value with no index. As i need to do some math operation with the selected cells. the function that i am using is:

import pandas as pd
 

data = [[82,1], [30, 2], [3.7, 3]]
 
df = pd.DataFrame(data, columns = ['Ah-Step', 'State'])
df['Ah-Step'].loc[df['State']==2]  df['Ah-Step'].loc[df['State']==3]

CodePudding user response:

.values[0] will do what OP wants.

Assuming one wants to obtain the value 30, the following will do the work

df.loc[df['State'] == 2, 'Ah-Step'].values[0]

print(df)

[Out]: 30.0

So, in OP's specific case, the operation 30 3.7 could be done as follows

df.loc[df['State'] == 2, 'Ah-Step'].values[0]   df['Ah-Step'].loc[df['State']==3].values[0]

[Out]: 33.7
  • Related