0

Here is the output of my code but now I want to extract the values of Check_In and Check_Out in variables so that I can perform own these values any suggestions on how to extract the values?

ID    Name        Check_In           Check_Out           
1     Zainab      7 am       5 pm
2   Abdullah      8 am       5 pm
3     Hassan      9 am       6 pm
4    Javeria     11 am       9 pm
5     Tayyab      7 am       5 pm
6     Fatima     11 am       9 pm

This is my code below:

import pandas as pd
import numpy as np
df = pd.read_csv("Book1.csv")
c = df.set_index('ID')
print(c)
f = int(input('Give ID number:' ))
t = (pd.DataFrame((c.loc[f])))
print(t)

This is the prompt:

Give ID number:2
>> 2

Name       Abdullah
Check_In        8am
Check_Out       5pm
mmustafaicer
  • 434
  • 6
  • 15
  • 1
    Please modify your post to make results into code, what you have tried, and desired results – ArchAngelPwn May 10 '22 at 17:03
  • please provide the full dataframes including headers, but it looks like you want a simple Series slicing… – mozway May 10 '22 at 17:04
  • You need to post your code. See [mre] and [How to make good reproducible pandas examples](/q/20109391/4518341). – wjandrea May 10 '22 at 17:06
  • Please modify your post to include sample output. It is not clear whether you want a list which contains values like `[Check_in, Check_out]` or you want two variables `Check_In`, `Check_Out` separately. – mmustafaicer May 11 '22 at 17:48
  • @Zainab Tahir, if you have found your answer in one of the below, can you please click the check mark to accept it as your solution. [what-to-do-when-i-get-answers](https://stackoverflow.com/help/someone-answers) – mmustafaicer May 20 '22 at 20:03

3 Answers3

1

You can use pandas.DataFrame.values after user input the choice of ID. Since .values returns a NumPy array, I had to use slice indexing. (Scenario if user enters 2 as input)

check_in = t.loc['Check_In'].values[0] 
check_out = t.loc['Check_Out'].values[0] 
print(check_in)
print(check_out) 

Output:

8:00 AM
5:00 PM
mmustafaicer
  • 434
  • 6
  • 15
  • import pandas as pd import numpy as np df=pd.read_csv("Book1.csv") c=df.set_index('ID') print(c) f=int(input('Give ID number:' )) t=(pd.DataFrame((c.loc[f]))) print(t) check_in = df['Check_In'].values check_out = df['Check_Out'].values print(check_out) this is my code may be you will be able to understand it more I want to extract the value of Check_In and check_out from variable t – Zainab Tahir May 11 '22 at 04:46
  • You should note that your variable `t` is **pandas.core.frame.DataFrame** type. So it is still DataFrame. If you run `print(t.values)` in your notebook, you will see it returns a list something like [['Hassan'], ['9:00 AM'], ['6:00 PM']]. Then you can do slicing `t.values[1:3]`. Just for the sake of efficiency, I ask you to put an image of what the output should look like in your original post. Still, it is not clear what you want. – mmustafaicer May 11 '22 at 17:39
0

This will set your index to "Name" as well as select an index that is a string instead of a int

df.set_index('Name', inplace = True)
df.loc[['Zainab']].squeeze()

You can remove the .squeeze() if you simply want to have a df of the results.

ArchAngelPwn
  • 2,891
  • 1
  • 4
  • 17
0

IUUC, you can use Series.loc

c = df.set_index('ID')
f = 2
t = c.loc[f]
check_in = t.loc['Check_In']
check_out = t.loc['Check_Out']

# without `t`
check_in = c.loc[f, 'Check_In']
print(check_in)
8 am

print(check_out)
5pm

Or you can use

check_ins = df.loc[df['ID'].eq(f), 'Check_In'].tolist()
print(check_ins)

['8 am']
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52