values = [5, 6,7,8 , 9, 11,12, 13, 14, 17, 18,19, 20, 21,22, 23,
24, 25, 26, 27, 41, 42, 44, 45, 46, 47]
s = pd.Series(values)
s1 = s.groupby(s.diff().gt(1).cumsum()).apply(lambda x: ','.join(x.astype(str)))
print (s1)
0: 5,6,7,8,9
1: 11,12,13,14
2: 17,18,19,20,21,22,23,24,25,26,27
3: 41,42
4: 44,45,46,47
I am trying to find the min
and max
of each line of the group. I have tried several approaches, but I don't get it correctly.
我试图找到该组每行的最小值和最大值。我尝试了几种方法,但我没有正确理解。
My belief is, it has to be converted to int
, then the maximum and minimum can be found, but I am not sure how to do that. Every time I try to access the series it converts to strings.
我的信念是,它必须转换为int,然后可以找到最大值和最小值,但我不知道该怎么做。每次我尝试访问该系列时,它都会转换为字符串。
The output will be in a form of min
and max
value in the following for
loop:
以下for循环中的输出将采用最小值和最大值的形式:
for num in s1:
min_value =
max_value =
print(min_value ,max_value )
4 个解决方案
#1
0
I suggest create list
s instead joined string
s and then use min
and max
:
我建议创建列表而不是连接字符串,然后使用min和max:
s1 = s.groupby(s.diff().gt(1).cumsum()).apply(list)
print (s1)
0 [5, 6, 7, 8, 9]
1 [11, 12, 13, 14]
2 [17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27]
3 [41, 42]
4 [44, 45, 46, 47]
dtype: object
for num in s1:
min_value = min(num)
max_value = max(num)
print(min_value ,max_value)
Or better use groupby
object and first join to string
s and then aggregate min
and max
:
或者更好地使用groupby对象并首先连接到字符串然后聚合min和max:
g = s.groupby(s.diff().gt(1).cumsum())
s1 = g.apply(lambda x: ','.join(x.astype(str)))
print (s1)
0 5,6,7,8,9
1 11,12,13,14
2 17,18,19,20,21,22,23,24,25,26,27
3 41,42
4 44,45,46,47
dtype: object
s1 = g.agg([min, max])
print (s1)
min max
0 5 9
1 11 14
2 17 27
3 41 42
4 44 47
But if need working with joined
strings is possible split and convert to int
, last get min
and max
:
但是如果需要使用连接字符串可以拆分并转换为int,则最后得到min和max:
s1 = s.groupby(s.diff().gt(1).cumsum()).apply(lambda x: ','.join(x.astype(str)))
print (s1)
0 5,6,7,8,9
1 11,12,13,14
2 17,18,19,20,21,22,23,24,25,26,27
3 41,42
4 44,45,46,47
dtype: object
for line in s1:
a = [int(x) for x in line.split(',')]
min_value = min(a)
max_value = max(a)
print(min_value ,max_value)
#2
0
One suggestion:
import pandas as pd
values = [5, 6,7,8 , 9, 11,12, 13, 14, 17, 18,19, 20, 21,22, 23,
24, 25, 26, 27, 41, 42, 44, 45, 46, 47]
s = pd.Series(values)
s1 = s.groupby(s.diff().gt(1).cumsum()).apply(lambda x: ','.join(x.astype(str)))
for line in s1:
print("{} -> max: {}, min: {}".format(line, max(line.split(',')), min(line.split(','))))
'''
5,6,7,8,9 -> max: 9, min: 5
11,12,13,14 -> max: 14, min: 11
17,18,19,20,21,22,23,24,25,26,27 -> max: 27, min: 17
41,42 -> max: 42, min: 41
44,45,46,47 -> max: 47, min: 44
'''
#3
0
After you get the s1
获得s1后
s2=s1.str.split(',',expand=True).apply(pd.to_numeric)
s2.max(1)
Out[29]:
0 9.0
1 14.0
2 27.0
3 42.0
4 47.0
dtype: float64
s2.min(1)
Out[30]:
0 5.0
1 11.0
2 17.0
3 41.0
4 44.0
dtype: float64
If you like int , you can adding astype(int)
at the end
如果你喜欢int,你可以在最后添加astype(int)
#4
0
Here's what you can do with apply
function
以下是使用apply函数可以执行的操作
min_max = s1.apply(lambda x: (min(map(int, x.split(','))),
max(map(int, x.split(',')))))
for min_, max_ in min_max:
print (min_, max_)
execution time:
In [10]: timeit s1.apply(lambda x: (min(map(int, x.split(','))), max(map(int, x.split(',')))))
109 µs ± 445 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
#1
0
I suggest create list
s instead joined string
s and then use min
and max
:
我建议创建列表而不是连接字符串,然后使用min和max:
s1 = s.groupby(s.diff().gt(1).cumsum()).apply(list)
print (s1)
0 [5, 6, 7, 8, 9]
1 [11, 12, 13, 14]
2 [17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27]
3 [41, 42]
4 [44, 45, 46, 47]
dtype: object
for num in s1:
min_value = min(num)
max_value = max(num)
print(min_value ,max_value)
Or better use groupby
object and first join to string
s and then aggregate min
and max
:
或者更好地使用groupby对象并首先连接到字符串然后聚合min和max:
g = s.groupby(s.diff().gt(1).cumsum())
s1 = g.apply(lambda x: ','.join(x.astype(str)))
print (s1)
0 5,6,7,8,9
1 11,12,13,14
2 17,18,19,20,21,22,23,24,25,26,27
3 41,42
4 44,45,46,47
dtype: object
s1 = g.agg([min, max])
print (s1)
min max
0 5 9
1 11 14
2 17 27
3 41 42
4 44 47
But if need working with joined
strings is possible split and convert to int
, last get min
and max
:
但是如果需要使用连接字符串可以拆分并转换为int,则最后得到min和max:
s1 = s.groupby(s.diff().gt(1).cumsum()).apply(lambda x: ','.join(x.astype(str)))
print (s1)
0 5,6,7,8,9
1 11,12,13,14
2 17,18,19,20,21,22,23,24,25,26,27
3 41,42
4 44,45,46,47
dtype: object
for line in s1:
a = [int(x) for x in line.split(',')]
min_value = min(a)
max_value = max(a)
print(min_value ,max_value)
#2
0
One suggestion:
import pandas as pd
values = [5, 6,7,8 , 9, 11,12, 13, 14, 17, 18,19, 20, 21,22, 23,
24, 25, 26, 27, 41, 42, 44, 45, 46, 47]
s = pd.Series(values)
s1 = s.groupby(s.diff().gt(1).cumsum()).apply(lambda x: ','.join(x.astype(str)))
for line in s1:
print("{} -> max: {}, min: {}".format(line, max(line.split(',')), min(line.split(','))))
'''
5,6,7,8,9 -> max: 9, min: 5
11,12,13,14 -> max: 14, min: 11
17,18,19,20,21,22,23,24,25,26,27 -> max: 27, min: 17
41,42 -> max: 42, min: 41
44,45,46,47 -> max: 47, min: 44
'''
#3
0
After you get the s1
获得s1后
s2=s1.str.split(',',expand=True).apply(pd.to_numeric)
s2.max(1)
Out[29]:
0 9.0
1 14.0
2 27.0
3 42.0
4 47.0
dtype: float64
s2.min(1)
Out[30]:
0 5.0
1 11.0
2 17.0
3 41.0
4 44.0
dtype: float64
If you like int , you can adding astype(int)
at the end
如果你喜欢int,你可以在最后添加astype(int)
#4
0
Here's what you can do with apply
function
以下是使用apply函数可以执行的操作
min_max = s1.apply(lambda x: (min(map(int, x.split(','))),
max(map(int, x.split(',')))))
for min_, max_ in min_max:
print (min_, max_)
execution time:
In [10]: timeit s1.apply(lambda x: (min(map(int, x.split(','))), max(map(int, x.split(',')))))
109 µs ± 445 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)