我正在处理一个数据帧df
,其中有许多列填充了数值
df
lorem ipsum | dolor sic | ... | (hundreds of cols)
---------------------------------------------------------
0.5 | -6.2 | ... | 79.8
-26.1 | 6200.0 | ... | -65.2
150.0 | 3.14 | ... | 1.008
换句话说,我有list_cols
列:
list_cols = ['lorem ipsum', 'dolor sic', ... ] # arbitrary length, of course len(list_cols ) <= len(df.columns), and contains valid columns of my df
我想获得2个数据帧:
value < 0
表示至少一个list_cols
(对应于OR
)。我们称之为negative_values_matches
positive_values_matches
对于list_cols = ['lorem ipsum', 'dolor sic']
,我将获得dataframes were least 1 value in list\u cols is strickly negative:
negative_values_matches
lorem ipsum | dolor sic | ... | (hundreds of cols)
---------------------------------------------------------
0.5 | -6.2 | ... | 79.8
-26.1 | 6200.0 | ... | -65.2
positive_values_matches
lorem ipsum | dolor sic | ... | (hundreds of cols)
---------------------------------------------------------
150.0 | 3.14 | ... | 1.008
我不想写这种代码:
negative_values_matches = df[ (criterion1 | criterion2 | ... | criterionn)]
positive_values_matches = df[~(criterion1 | criterion2 | ... | criterionn)]
(其中criterionk
是对列k
的布尔求值,例如:(df[col_k]>=0)
,此处使用括号,因为它是Pandas语法)
这个想法是要有一个程序化的方法。 我主要寻找布尔数组,这样就可以使用布尔索引(参见Pandas documentation)。你知道吗
据我所知,这些帖子并不完全是我所说的:
我不知道如何用OR
操作符将我的数据帧上的布尔值连接起来,并获得正确的行分割。你知道吗
我能做什么?你知道吗
经过几次尝试,我终于达到了目标。你知道吗
代码如下:
这样,我成功地获得了2个数据帧:
list_cols
中至少有1列的值为<;0的所有行为1list_col
中的每一列,值>;=0)(数组初始化为False取决于布尔值选项)
注意:这种方法可以与multiple conditions on dataframes结合使用。待确认。你知道吗
相关问题 更多 >
编程相关推荐