Python Pandas DataFrame:在多列条件下对数据帧进行编程行拆分 上下文

Python Pandas DataFrame:在多列条件下对数据帧进行编程行拆分 上下文,python,pandas,dataframe,Python,Pandas,Dataframe,我正在处理一个数据帧df,其中有很多列填充了数值 df lorem ipsum | dolor sic | ... | (hundreds of cols) --------------------------------------------------------- 0.5 | -6.2 | ... | 79.8 -26.1 | 6200.0 | ... | -65.2 150.0 | 3.1

我正在处理一个数据帧
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 = ['lorem ipsum', 'dolor sic', ... ]  # arbitrary length, of course len(list_cols ) <= len(df.columns), and contains valid columns of my df

我不想为自己编写这种代码:

negative_values_matches = df[ (criterion1 | criterion2 | ... | criterionn)]
positive_values_matches = df[~(criterion1 | criterion2 | ... | criterionn)]
(其中,
criterionk
是对列
k
的布尔求值,例如:
(df[col\u k]>=0)
,此处使用括号,因为其语法是正确的)

我们的想法是采用一种程序化的方法。 我主要寻找布尔数组,这样我就可以使用布尔索引(请参阅)

据我所知,这些帖子并不完全是我所说的:

  • 这个离我要找的有点近。然而,它依赖于生成一个可能无法与“外来”列名(空格)一起工作的字符串(或者至少我不知道如何做)
我不知道如何使用
操作符将数据帧上的布尔值全部链接起来,并获得正确的行分割


我能做什么?

经过几次尝试,我终于实现了目标

代码如下:

import Pandas
import numpy
# assume dataframe exists
df = ...
# initiliaze an array of False, matching df number of rows
resulting_bools = numpy.zeros((1, len(df.index)), dtype=bool)

for col in list_cols:
    # obtain array of booleans for given column and boolean condition for [row, column] value
    criterion = df[col].map(lambda x: x < 0) # same condition for each column, different conditions would have been more difficult (for me)

     # perform cumulative boolean evaluation accross columns
    resulting_bools |= criterion

# use the array of booleans to build the required df
negative_values_matches = df[ resulting_bools].copy() # use .copy() to avoid further possible warnings from Pandas depending on what you do with your data frame
positive_values_matches = df[~resulting_bools].copy()
导入熊猫
进口numpy
#假设数据帧存在
df=。。。
#初始化与行数匹配的False数组
结果布尔=numpy.zeros((1,len(df.index)),dtype=bool)
对于列表中的列:
#获取给定列的布尔值数组和[row,column]值的布尔条件
criteria=df[col].map(λx:x<0)#每个列的条件相同,不同的条件会更难(对我来说)
#跨列执行累积布尔求值
结果_bools |=标准
#使用布尔数组构建所需的df
负值\u matches=df[resulting\u bools].copy()#根据您对数据帧的操作,使用.copy()避免熊猫发出进一步的警告
正值匹配=df[~结果值].copy()
这样,我成功地获得了2个数据帧:

  • 对于
    列表中至少一列的值<0的所有行,1
  • 1与所有其他行(对于
    列表中的每个列,值>=0)
(数组初始化为False取决于布尔求值选项)


注:该方法可与其他方法相结合。待确认

import Pandas
import numpy
# assume dataframe exists
df = ...
# initiliaze an array of False, matching df number of rows
resulting_bools = numpy.zeros((1, len(df.index)), dtype=bool)

for col in list_cols:
    # obtain array of booleans for given column and boolean condition for [row, column] value
    criterion = df[col].map(lambda x: x < 0) # same condition for each column, different conditions would have been more difficult (for me)

     # perform cumulative boolean evaluation accross columns
    resulting_bools |= criterion

# use the array of booleans to build the required df
negative_values_matches = df[ resulting_bools].copy() # use .copy() to avoid further possible warnings from Pandas depending on what you do with your data frame
positive_values_matches = df[~resulting_bools].copy()