pandas 剪切DataFrame并获取负值

f45qwnt8  于 2023-10-14  发布在  其他
关注(0)|答案(1)|浏览(115)

我有一个DataFrame,我需要剪切它并从负值中获得min:
然后我需要将本地最小值导出为像min_lokal = [-1.3,-2,.....]这样的数组,还有一个问题:我是怎么得到这个价格的?enter image description here
数据类型:

df = pd.DataFrame({'col': [4, 5, -0.8,-1.3, -0.7, -.5, .4, 5, -.9, -.6, -2]})
nnt7mjpx

nnt7mjpx1#

GroupBy.min用于由Series.cumsum创建的组:

m = df['col'].gt(0)
out = df[~m].groupby(m.cumsum())['col'].min().tolist()
print (out)
[-1.3, -2.0]

编辑:

df = pd.DataFrame({'price': [1,2,3,5,6,8,7,1,2,3,4],
                   'min_lokal': [4, 5, -0.8,-1.3, -0.7, -.5, .4, 5, -.9, -.6, -2]})
print (df)
    price  min_lokal
0       1        4.0
1       2        5.0
2       3       -0.8
3       5       -1.3
4       6       -0.7
5       8       -0.5
6       7        0.4
7       1        5.0
8       2       -0.9
9       3       -0.6
10      4       -2.0

m = df['min_lokal'].gt(0)
out = df[~m].groupby(m.cumsum())['min_lokal'].agg(['min','idxmin'])
print (out)
           min  idxmin
min_lokal             
2         -1.3       3
4         -2.0      10

print (out['min'].tolist())
[-1.3, -2.0]

print (df.loc[out['idxmin'], 'price'].tolist())
[5, 4]

相关问题