1

I have the following data frame:

Exp          Variable  Score  all           best            rsmin
ctr          Qle       MBE    -3.061518      0.082860       -3.921793
ctr          Qh        MBE    12.275757      0.464946       12.288968
ctr          NEE       MBE     0.000022      0.000013       0.000015
ctr          Qle       SD      0.223095      0.010868       0.052601
ctr          Qh        SD      0.170328      0.065823       0.147554
ctr          NEE       SD      0.361303      0.223580       0.327189

I am reading the dataframe as follow:

df = pd.read_csv(inbasedir + "median_scores.csv", delimiter=",")

I am removing the number of the columns as follow:

df.set_index('Exp', inplace=True)
df.columns.name = df.index.name
df.index.name = None
index=df.index

Now I would like to select a single value, let say the value linked to ctr, Qle, MBE, best which is egal to 0.082860, for that I am using:

a=df.loc[['ctr'],['Qle','MBE','best']].values

but it returns me the entir 'best' column:

0.082860
0.464946
0.000013
0.010868
0.065823
0.223580

Does anyone know how to select a single value?

Georgy
  • 12,464
  • 7
  • 65
  • 73
steve
  • 511
  • 1
  • 9
  • 33
  • You can select based on position using `df.iloc`. In your scenario, if you want the first row and first value of the `best` column, it would be: `df.iloc[0][4]` – Simon May 22 '19 at 15:40

2 Answers2

2

you can try:

df.loc[(df['Exp'] == 'ctr') & (df['Variable'] == 'Qle') & (df['Score'] == 'MBE'), 'best'].values
Benjamin Breton
  • 1,388
  • 1
  • 13
  • 42
1

Try using a multiindex, right now you are using "Exp" as the index which doesn't have unique values. That's why it's returning the entire best column.

df.set_index(['Exp','Variable','Score'],inplace=True)

a=df.loc[('ctr','Qle','MBE'),'best'].values
iamchoosinganame
  • 1,090
  • 6
  • 15