Summarizing data in a dictionary

I have a pandas data frame like

    date        hour  level
0  2015-10-27    0     2.5
1  2015-10-27    1     2.5     
2  2015-10-27    2     2.5
3  2015-10-27    3     2.5
4  2015-10-28    0     0.0
5  2015-10-28    1     0.0
6  2015-10-28    2     0.0
7  2015-10-28    3     0.0
8  2015-10-28    4     0.0
...
14 2015-10-29    0     3.23
15 2015-10-29    1     3.23
...

houralways starts at 0 and increases by 1; it may or may not reach 23. it dateincreases one day at a time, but the number of date entries may differ from date to date, for example. - 2015-10-27has 4 entries, and 2015-10-28- up to line 13. The element levelwill always have the same value for this date, but this value may or may not be displayed for another date.

What I'm trying to get is a dictionary of this form

{'2015-10-27': '3', ..., '2015-10-29': '4', ...}

, level, .. level 2,5, [2, 3]. , , - "" , 10 if ( 9.xx) , , , , , .

?

+4
1

level , date ( DataFrame.groupby()), max level, ( ), Series.to_dict(). -

df.groupby('date')['level'].first().apply(np.ceil).to_dict()

-

In [44]: df
Out[44]:
          date  hour  level
0   2015-10-27     0   2.50
1   2015-10-27     1   2.50
2   2015-10-27     2   2.50
3   2015-10-27     3   2.50
4   2015-10-28     0   0.00
5   2015-10-28     1   0.00
6   2015-10-28     2   0.00
7   2015-10-28     3   0.00
8   2015-10-28     4   0.00
14  2015-10-29     0   3.23
15  2015-10-29     1   3.23

In [45]: df.groupby('date')['level'].first().apply(np.ceil).to_dict()
Out[45]: {'2015-10-27': 3.0, '2015-10-28': 0.0, '2015-10-29': 4.0}

, int, int Series.astype(). -

In [46]: df.groupby('date')['level'].first().apply(np.ceil).astype(int).to_dict()
Out[46]: {'2015-10-27': 3, '2015-10-28': 0, '2015-10-29': 4}
+2

Source: https://habr.com/ru/post/1613302/


All Articles