gpt4 book ai didi

python - Pandas:如何提取匹配 Filter1 或 filter2 的数据帧的行

转载 作者:太空宇宙 更新时间:2023-11-04 10:24:54 24 4
gpt4 key购买 nike

例如,我有一个看起来像这样的 pandas 数据框:

label          Y88_N          diff       div      fold  
0 25273.626713 17348.581851 2.016404 2.016404
1 29139.510491 -4208.868050 0.604304 -0.604304
2 34388.439717 -30147.834699 0.458903 -0.458903
3 69704.254089 -32976.152490 0.116894 -0.116894
4 193717.440783 -71359.494098 0.286045 -0.286045
5 28996.634708 10934.944533 2.031293 2.031293
6 45021.782930 680.437629 1.056383 1.056383

但有数千行。当“折叠”列中的值时,我想获得一个包含行的新数据框> 2 或 < 0.6。所以最后数据框应该是这样的:

label          Y88_N          diff       div      fold  
0 25273.626713 17348.581851 2.016404 2.016404
1 29139.510491 -4208.868050 0.604304 -0.604304
5 28996.634708 10934.944533 2.031293 2.031293

我尝试过不同的方法,例如:

def ranged(start, end, step):
x = start
while x < end:
yield x
x += step
df2 = df[~df['fold'].isin(ranged(-0.6, 2, 0.000001))]

df2 = df[(df['fold'] >= 2) & (df['fold'] <= -0.6)]

但似乎没有任何效果有没有一种简单的方法可以选择与过滤器 1 或过滤器 2 匹配的列中的值?谢谢

最佳答案

你可以做

In [276]: df[(df['fold'] >= 2) | (df['fold'] <= -0.6)]
Out[276]:
label Y88_N diff div fold
0 0 25273.626713 17348.581851 2.016404 2.016404
1 1 29139.510491 -4208.868050 0.604304 -0.604304
5 5 28996.634708 10934.944533 2.031293 2.031293

或者使用类似query的方法

In [277]: df.query('fold >=2 | fold <=-0.6')
Out[277]:
label Y88_N diff div fold
0 0 25273.626713 17348.581851 2.016404 2.016404
1 1 29139.510491 -4208.868050 0.604304 -0.604304
5 5 28996.634708 10934.944533 2.031293 2.031293

而且,pd.eval() 适用于包含大型数组的表达式

In [278]: df[pd.eval('df.fold >=2 | df.fold <=-0.6')]
Out[278]:
label Y88_N diff div fold
0 0 25273.626713 17348.581851 2.016404 2.016404
1 1 29139.510491 -4208.868050 0.604304 -0.604304
5 5 28996.634708 10934.944533 2.031293 2.031293

关于python - Pandas:如何提取匹配 Filter1 或 filter2 的数据帧的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29935056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com