python - 如何在 Pandas 中添加行切片?
问题描述
我有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
先加入然后加入它们?
解决方案
...
...
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])
推荐阅读
- javascript - Rendering different child components based on data recieved , in a specific order
- python - Selenium Webdriver can't access a website (The requested URL was rejected)
- md5 - 有没有办法在 md5 文件中添加注释?
- python - 检查 discord 中的消息内容以查找 discord.py 中的特定单词
- javascript - 如何获取没有表单ID的html表单
- reactjs - 使用创建反应应用程序和可加载组件的服务器端渲染和代码分割
- spring-webflux - Spring Cloud Gateway 向客户端隐藏服务器 websocket 握手 401 失败
- python-3.x - ModuleNotFoundError:没有名为“wagtail”的模块
- reactjs - 无法使用 react-cropper 创建 Cropperjs 实例
- if-statement - Lisp 宏多任务