简体   繁体   English

按熊猫系列中的两列排序

[英]Sorting by two columns in pandas series

Putting a slight variation on a question I previously asked. 在我之前提出的问题上稍作改动。 I managed to get a solution to sorting values by a particular column in my pandas series. 我设法找到一种解决方案,可以按我的熊猫系列中的特定列对值进行排序。 However, the problem is that sorting purely by time doesn't allow me to factor in different dates in which the time occurred. 但是,问题在于,纯粹按时间排序无法让我考虑发生时间的不同日期。 I understand that I could potentially hard code the order and use .loc to apply the order but wanted to find out if there was a simpler solution to sort primarily by week (earliest week first) and by time (0-23hours for each week). 我知道我可以对订单进行硬编码并使用.loc来应用订单,但想了解是否存在一种更简单的解决方案,可以按周(最早的一周先到)和按时间(每周0-23小时)进行排序。

Here is a sample of the dataframe I have again: 这是我再次拥有的数据框的示例:

weeknum   time_hour
16-22Jun  0.0           5
2-8Jun    0.0           3
23-29Jun  0.0          11
9-15Jun   0.0           3
16-22Jun  1.0           3
2-8Jun    1.0           6
23-29Jun  1.0           3
9-15Jun   1.0           8
16-22Jun  2.0           3
2-8Jun    2.0           6
23-29Jun  2.0           3
16-22Jun  3.0           4
2-8Jun    3.0           2
23-29Jun  3.0           3
9-15Jun   3.0           4
16-22Jun  4.0           2
2-8Jun    4.0           7
23-29Jun  4.0           1
9-15Jun   4.0           7
16-22Jun  5.0           2
2-8Jun    5.0           9
23-29Jun  5.0           9
9-15Jun   5.0          12
16-22Jun  6.0           5
2-8Jun    6.0          12
23-29Jun  6.0           6
9-15Jun   6.0          14
16-22Jun  7.0          12
2-8Jun    7.0          17
23-29Jun  7.0          19

This is my code: 这是我的代码:

merged_clean.groupby('weeknum')['time_hour'].value_counts().sort_index(level=['time_hour'])

Use function sorted by multiple keys for sorting MultiIndex with convert first number before - and for change order use DataFrame.reindex : 使用按多个键排序的函数对MultiIndex进行排序,并在-之前转换第一个数字;对于更改顺序,请使用DataFrame.reindex

s = merged_clean.groupby('weeknum')['time_hour'].value_counts()
idx = sorted(s.index, key = lambda x: (int(x[0].split('-')[0]), x[1]))
s = s.reindex(idx)

print (s)
weeknum   time_hour
2-8Jun    0.0           3
          1.0           6
          2.0           6
          3.0           2
          4.0           7
          5.0           9
          6.0          12
          7.0          17
9-15Jun   0.0           3
          1.0           8
          3.0           4
          4.0           7
          5.0          12
          6.0          14
16-22Jun  0.0           5
          1.0           3
          2.0           3
          3.0           4
          4.0           2
          5.0           2
          6.0           5
          7.0          12
23-29Jun  0.0          11
          1.0           3
          2.0           3
          3.0           3
          4.0           1
          5.0           9
          6.0           6
          7.0          19
Name: a, dtype: int64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM