Search code examples
pythonpandasdataframeindexingspyder

Printing specific columns for those that satisfy a condition in Pandas data frame (Code works, Just need help to reduce it to one line of code)


Here's a quick explanation of the original Data Frame named df (taken from a csv):

  • Columns: Name, Age, Gender, Average Sleep Time, Average Wake Time
  • Rows: 100 samples

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?


Solution

  • 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")