作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个数据框,如下面的 df。我想为条件为真的每个数据 block 创建一个新的数据帧,以便返回 df_1、df_2....df_n。
| df | | df_1 | | df_2 |
| Value | Condition | | Value | | Value |
|-------|-----------| |-------|---|-------|
| 2 | True | | | 2 | | 0 |
| 5 | True | | | 5 | | 5 |
| 4 | True | | | 4 | | |
| 4 | False | | | | | |
| 2 | False | | | | | |
| 0 | True | | | | | |
| 5 | True | | | | | |
| 7 | False | | | | | |
| 8 | False | | | | | |
| 9 | False | | | | | |
我唯一的想法是遍历数据帧,返回每个真值 block 的开始和结束索引,然后创建新的数据帧,循环遍历返回的索引,为每个开始/结束对返回类似这样的内容:
newdf = df.iloc[start:end]
但这样做似乎效率不高。
最佳答案
这是另一种解决方案。注意 consecutive_groups
食谱来自more_itertools图书馆。
from itertools import groupby
from operator import itemgetter
def consecutive_groups(iterable, ordering=lambda x: x):
for k, g in groupby(enumerate(iterable), key=lambda x: x[0] - ordering(x[1])):
yield map(itemgetter(1), g)
grps = consecutive_groups(df[df.Condition].index)
dfs = {i: df.iloc[list(j)] for i, j in enumerate(grps, 1)}
# {1: Value Condition
# 0 2 True
# 1 5 True
# 2 4 True,
# 2: Value Condition
# 5 0 True
# 6 5 True}
关于python - 当条件为真时,Pandas 将数据帧拆分为多个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48770013/
我是一名优秀的程序员,十分优秀!