【发布时间】:2020-03-04 15:42:22
【问题描述】:
我有一个数据框列:
df = pd.DataFrame({'a':[1,3,5,7,4,5,6,4,7,8,9]})
我需要的输出列表是(保持范围的最小最大值):
[[1,4],[5,7],[8,9]]
这是我走了多远:
import pandas as pd
df = pd.DataFrame({'a':[1,3,5,7,4,5,6,4,7,8,9]})
# Convert df column to a unique value list and sort ascending
us = df['a'].unique().tolist()
us.sort()
lst1 = [int(v) for v in us]
# Create 3 groups of values
lst2 = [lst1[i:i + 3] for i in xrange(0, len(lst1), 3)]
# Keep only min and max of these groups
如何转换:
[[1,3,4],[5,6,7],[8,9]]
到我想要的输出?
【问题讨论】: