Pandas Dataframe group by week of week

时间:2017-10-11 13:20:43

标签: pandas time-series

我有一个数据框,其中包含一个房子(ID)的每小时kWh能耗(消耗),持续几个月,例如:

                      ID  Consumption
DateTime                              
2016-07-01 01:00:00  1642     0.703400
2016-07-01 02:00:00  1642     0.724033
2016-07-01 03:00:00  1642     0.747300
2016-07-01 04:00:00  1642     0.830450
2016-07-01 05:00:00  1642     0.704917
2016-07-01 06:00:00  1642     0.708467
2016-07-01 07:00:00  1642     0.806533
2016-07-01 08:00:00  1642     0.774483
2016-07-01 09:00:00  1642     0.724833
2016-07-01 10:00:00  1642     0.721900
2016-07-01 11:00:00  1642     0.729450
2016-07-01 12:00:00  1642     0.757233
2016-07-01 13:00:00  1642     0.744667

这里DateTime是类型的索引。我的目标是找到一周内每小时的平均消费和差异,即(24 * 7 = 168小时)

HourOfWeek    Consumption
1             0.703400
2             0.724033
...
168           0.876923

我试过了

print (df.groupby(df.index.week)['Consumption'].transform('mean'))

然而,这并没有给出正确的结果,如何在大熊猫中完成?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

我认为groupbydayofweek需要1,但需要添加0,因为两者中的第一个值都是mean。然后汇总df1 = (df.groupby((df.index.dayofweek + 1) * (df.index.hour + 1))['Consumption'].mean() .rename_axis('HourOfWeek') .reset_index()) print (df1) HourOfWeek Consumption 0 10 0.703400 1 15 0.724033 2 20 0.747300 3 25 0.830450 4 30 0.704917 5 35 0.708467 6 40 0.806533 7 45 0.774483 8 50 0.724833 9 55 0.721900 10 60 0.729450 11 65 0.757233 12 70 0.744667

 ArrayList<Favoritos> favorites;
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View rootView = inflater.inflate(R.layout.list_item, container, false);
    favorites = new ArrayList<Favoritos>();
    favorites.add(new Favorites("Title", "addres", 5, false, 7, 22, 4.5f));
    favorites.add(new Favorites("Title", "addres", 3.5 , true, 7, 22, 3.0f));
    favorites.add(new Favorites("Title", "addres", 2, false, 7, 22, 4.0f));
    favorites.add(new Favorites("Titleo", "addres", 4.5, true, 7, 22, 5.0f));

    FavoritesAdapter adapter = new FavoritesAdapter(getActivity(), favorites);

    ListView listView = (ListView) rootView.findViewById(R.id.list_item);

    listView.setAdapter(adapter);
 }


 @Override
public boolean onQueryTextSubmit(String s) {
    return false;
}

@Override
public boolean onQueryTextChange(String string) {
    search(string);
    return true;
}

//This is the search method
public void search(String s) {
    if (s == null || s.trim().equals("")) {
        cleanSearch(); //In this method I just "reset" the adapter when the user "give up"
        return;
    }

    ArrayList<Favoritos> estacionamentosEncontrados = new ArrayList<Favoritos>(favoritos);

    for (int i = estacionamentosEncontrados.size() - 1; i >= 0; i--) {
        Favoritos estacionamento = estacionamentosEncontrados.get(i);
        Favoritos nenhumResultado = new Favoritos("Nenhum resultado encontrado");
        if (!estacionamento.mIndicacao.toUpperCase().contains(s.toUpperCase())) {
            estacionamentosEncontrados.remove(estacionamento);
        } else {
            estacionamentosEncontrados.add(nenhumResultado);
        }
    }

    adapter = new EncontradosAdapter(getActivity(), estacionamentosEncontrados);
    ListView listView = (ListView) getActivity().findViewById(R.id.list_item);
    listView.setAdapter(adapter);
}

答案 1 :(得分:0)

即使迟到:我也遇到了类似的问题,我认为以上答案不正确,应该是

df.groupby((df.index.dayofweek) * 24 + (df.index.hour)).mean().rename_axis('HourOfWeek')

在上述答案中,由于分配的组不是唯一的(例如,星期一2pm和星期二1 am依此类推,依此类推