我有一些数据的数据帧df
。而且我还有一个条件为selection
的切片cond_1
。
def cond1(row):
if some_condition_1:
return True
return False
def cond2(row):
if some_condition_2:
return True
return False
selection = df[df.apply(cond1, axis=1)]
# Some logic here
#...
#
# Later:
但是我需要使用其他条件cond2
附加一些数据
那么我可以只使用selection
扩展cond2
还是让我selection2
然后加入他们?
答案 0 :(得分:0)
...
...
selection1 = dataframe based on some condition
selection2 = dataframe based on some condition
selection1 = selection1[selection1.apply(cond1, axis=1)]
selection2 = selection2[selection2.apply(cond2, axis=1)]
pd.concat([selection1, selection2])
答案 1 :(得分:0)
您可以这样做
ind_cond1 = df.apply(cond1, axis=1)
ind_cond2 = df.apply(cond2, axis=1)
## if you want to slice df which matches either of the condition
or_cond = ind_cond1 | ind_cond2
df[or_cond]
## if you want to slice df which matches both condition
and_cond = ind_cond1 & ind_cond2
df[and_cond]
## you can also use other logical operation like xor, not etc