pandas - 熊猫,每 x 行取决于其他行的值
问题描述
我有一个数据集,其中的值在不同的行中都是父项和子项。父母和孩子的 ID 格式略有不同,所以我应该能够用正则表达式识别它们。
所以结构是这样的
Parent ID | Other data
Child ID | Other data
Child ID | Other data
Child ID | Other data
Parent ID | Other data
Child ID | Other data
Parent ID | Other data
Child ID | Other data
Child ID | Other data
Child ID | Other data
没有固定数量的孩子,但唯一始终正确的是,父母会先出现,然后是孩子,然后是下一个父母,然后是孩子,依此类推。
我不确定如何识别这一点。理想情况下,我能够遍历行,并在不同的(新)行中用父母的 ID 标记所有孩子。
它不是一个很好的结构,但它来自数据源。
我想要这样的输出
Parent ID | Other data
Child ID | Other data | Parent ID
Child ID | Other data | Parent ID
Child ID | Other data | Parent ID
Parent ID | Other data |
Child ID | Other data | Parent ID
Parent ID | Other data |
Child ID | Other data | Parent ID
Child ID | Other data | Parent ID
Child ID | Other data | Parent ID
所以整个文件,数千行,遵循这种格式,首先列出父级,所有子级,然后是下一个父级。
解决方案
ffill
你当然可以用一些掩蔽来做到这一点
# identify all parents
# replace with your regex
patt = '(Parent)'
is_parent = df['ID'].str.extract(patt).notnull()[0]
# ids:
df['parent_ID'] = df['ID'].where(is_parent).ffill().mask(is_parent)
输出:
ID data ParentID
0 Parent ID Other data NaN
1 Child ID Other data Parent ID
2 Child ID Other data Parent ID
3 Child ID Other data Parent ID
4 Parent ID Other data NaN
5 Child ID Other data Parent ID
6 Parent ID Other data NaN
7 Child ID Other data Parent ID
8 Child ID Other data Parent ID
9 Child ID Other data Parent ID
推荐阅读
- python - statsmodel 中线性回归常数的左侧 p 值
- python-3.x - 使用字符串进行操作:Python 中任何分隔符后的每个单词都大写
- angular - 将 webpack 的 `require.context` 与 angular cli 和 ivy 一起使用
- c# - 刚体游戏对象在移动时被限制移动
- c - 为什么使用 n=sizeof(myarray)/sizeof(myarray[0]) 会给出“注释”错误,但为 n 指定数值不会?
- reference - 在 STM32CubeIDE(基于 Eclipse 的 IDE)中添加新的源文件和头文件不能无缝工作
- objective-c - 如何在 Swift 中使用 Objective-C 可视化工具
- c# - 处理单例和静态对象
- prestashop - 使用 smarty 从模板中获取元素 id - Prestahop
- react-admin - 了解 React-Admin 翻译