熊猫:使用第二列列表的query()groupby()mean()

时间:2020-10-28 11:29:47

标签: python pandas pandas-groupby

我正在尝试解密一些继承的熊猫代码,无法确定列表 [[''DemandRate','DemandRateQtr','AcceptRate']] 在此代码行中的作用:< / p>

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

谁能告诉我列表的作用?

1 个答案:

答案 0 :(得分:1)

它是按列名过滤的,这里仅汇总列表中的列。

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

如果还有其他类似此列表的列,则by列表(此处为['quote_date'])中的列将被省略:

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