在数据框中添加新行,并按条件拆分前一行 [英] Adding new row in dataframe with conditional split of previous row

查看:105
本文介绍了在数据框中添加新行,并按条件拆分前一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果特定列值中存在或"条件,则必须通过数据帧中的先前值来拆分并创建新行.尝试使用拆分和连接无法获得所需的输出任何帮助
输入

if 'or' condition is present in a particular column value it has to split and create a new row by previous values in a data frame .tried with split and concate unable to get desired output any help is highly appreciated
input

  col1   col2           col3              col4
0  x     bca or cba     value1 or null    x1 or x2
1  y     bca            value2            x1

输出

   col1   col2           col3        col4
 0  x     bca            value1      x1
 1  x     cba            null        x2
 2  y     bca            value2      x1

推荐答案

使用:

cols = df.columns

df = (df.join(df.pop('col2')
        .str.split(' or ', expand=True)
        .stack()
        .reset_index(level=1, drop=True)
        .rename('col2'))
      ).reset_index(drop=True).reindex(columns=cols)
print (df)
  col1 col2    col3
0    x  bca  value1
1    x  cba  value1
2    y  bca  value2

说明:

  1. 第一 pop 列用于使用 split 进行提取expand = TrueDataFrame
  2. 的列
  3. 通过 stack
  4. reset_index 进行删除MultiIndex
  5. 的第一级
  6. rename Series通过新列名
  7. 然后 join 原始df
  8. 函数 reset_index 是唯一索引所必需的
  9. 最后一个 reindex 列名顺序相同
  1. First pop column for extract with split column with expand = True to DataFrame
  2. Reshape by stack
  3. reset_index for remove first level of MultiIndex
  4. rename Series by new column name
  5. Then join to original df
  6. Function reset_index is necessary for unique index
  7. Last reindex for same order of columns names

df = (df.set_index('col1')
        .stack()
        .str.split(' or ', expand=True)
        .stack()
        .unstack(1)
        .reset_index(level=1, drop=True)
        .reset_index()

)
print (df)

  col1 col2    col3 col4
0    x  bca  value1   x1
1    x  cba    null   x2
2    y  bca  value2   x1

这篇关于在数据框中添加新行,并按条件拆分前一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆