bumblebear
bumblebear

Reputation: 95

Pandas: query() groupby() mean() using second column list

I'm trying to decypher some inherited pandas code and cannot determine what the list [['DemandRate','DemandRateQtr','AcceptRate']] is doing in this line of code:

plot_data = (my_dataframe.query("quote_date>'2020-02-01'")
                         .groupby(['quote_date'])[['DemandRate', 'DemandRateQtr', 'AcceptRate']]
                         .mean()
                         .reset_index()
            )

Can anyone tell me what the list does?

Upvotes: 1

Views: 85

Answers (1)

jezrael
jezrael

Reputation: 863611

It is filter by columns names, here are aggregate only columns from list.

['DemandRate', 'DemandRateQtr', 'AcceptRate']

If there are some another columns like this list and from by list(here ['quote_date']) are omitted:

my_dataframe = pd.DataFrame({
        'quote_date':pd.date_range('2020-02-01', periods=3).tolist() * 2,
         'DemandRate':[4,5,4,5,5,4],
         'DemandRateQtr':[7,8,9,4,2,3],
         'AcceptRate':[1,3,5,7,1,0],
         'column':[5,3,6,9,2,4]
})

print(my_dataframe)
  quote_date  DemandRate  DemandRateQtr  AcceptRate  column
0 2020-02-01           4              7           1       5
1 2020-02-02           5              8           3       3
2 2020-02-03           4              9           5       6
3 2020-02-01           5              4           7       9
4 2020-02-02           5              2           1       2
5 2020-02-03           4              3           0       4

plot_data = (my_dataframe.query("quote_date>'2020-02-01'")
                         .groupby(['quote_date'])[['DemandRate', 'DemandRateQtr', 'AcceptRate']]
                         .mean()
                         .reset_index())
print (plot_data)

  #here is not column
  quote_date  DemandRate  DemandRateQtr  AcceptRate
0 2020-02-02         5.0            5.0         2.0
1 2020-02-03         4.0            6.0         2.5

Upvotes: 1

Related Questions