在数据框中添加新行,并按条件拆分前一行 [英] Adding new row in dataframe with conditional split of previous row
本文介绍了在数据框中添加新行,并按条件拆分前一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果特定列值中存在或"条件,则必须通过数据帧中的先前值来拆分并创建新行.尝试使用拆分和连接无法获得所需的输出任何帮助
输入
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
说明:
- 第一
pop
列用于使用split
进行提取expand = True
到DataFrame
的列
- 通过
stack
-
reset_index
进行删除MultiIndex
的第一级
-
rename
Series
通过新列名 - 然后
join
原始df - 函数
reset_index
是唯一索引所必需的 - 最后一个
reindex
列名顺序相同
- First
pop
column for extract withsplit
column withexpand = True
toDataFrame
- Reshape by
stack
reset_index
for remove first level ofMultiIndex
rename
Series
by new column name- Then
join
to original df - Function
reset_index
is necessary for unique index - 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屋!
查看全文