Home > other >  how to extract whole row using .loc or .iloc function if index is non-integer value
how to extract whole row using .loc or .iloc function if index is non-integer value

Time:05-11

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
Give ID number:2
                  2
Name       Abdullah
Check_In        8am
Check_Out       5pm

CodePudding user response:

I believe you want to extract two columns as separate variables so you can perform operations on that (need clarification).

You can use pandas.DataFrame.values

check_in = df['Check_In'].values
check_out = df['Check_Out'].values

This will return a numpy array.

CodePudding user response:

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.

  • Related