0

I have the following dataframe:

import pandas as pd
#Create DF
d = { 
     'Day': ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'],
    'Staff Count':[20,18,17,16,15,7,6,],
    }
           
df = pd.DataFrame(data=d)
df

enter image description here

I would simply like to return the staff count on Mondays to a variable 20.

I have tried:

MonStaff = df.lookup(df.index[df.Day=='Mon'],df['Staff Count'])

to no avail but I'm sure it's something simple!

Thanks in advance!

Raju Ahmed
  • 1,267
  • 2
  • 13
  • 23
SOK
  • 1,522
  • 1
  • 6
  • 21
  • 3
    `df.loc[df.Day == 'Mon', 'Staff Count']` ? To get as a scalar value: `df.loc[df.Day == 'Mon', 'Staff Count'].iat[0]` – Psidom Jan 23 '22 at 05:31
  • `MonStaff = df.loc[df.Day == 'Mon', 'Staff Count'][0]` worked great thanks! if you put the solution up i can accept it! – SOK Jan 23 '22 at 06:06

1 Answers1

0

Transform your dataframe into a Series indexed by day and use simple indexing:

s = df.set_index('Day')['Staff Count']

s['Mon']
# 20

Or, if you have multiple columns:

df2 = df.set_index('Day')

df2.loc['Mon', 'Staff Count']
# 20

This will be efficient on the long run if you need to fetch multiple values.

mozway
  • 81,317
  • 8
  • 19
  • 49