Here's a quick explanation of the original Data Frame named df (taken from a csv):
What I tried:
I need to print a new data frame that only selects those who are female, and print their Average Sleep Time and Average Wake Time.
I managed to achieve this will multiples lines of code:
female = df[df['Gender'] == 'Female']
female.set_index("Name", inplace = True)
female[['average_sleep_time', 'average_wake_time']]
The indexing was done since the normal data frame, df, had no index.
Running the last line of code after running the ones above printed my target data frame successfully. Now my question is this: How can I do the same, with just one line of code?
Use DataFrame.loc
with DataFrame.set_index
without inplace=True
:
df.loc[df['Gender'] == 'Female', ['Name','average_sleep_time', 'average_wake_time']].set_index("Name")