[英]How to expand rows based on a particular row value in a dataframe?
I have a dataframe as follows:我有一个 dataframe 如下:
Col1 Col2 Col3 Col4
AB 2i 2j|2k 2y
CD 3j 3k|3p|3e 3x
So, for those rows which have pipeline separated values, I want to expand the rows as follows: (This should be the final dataframe)因此,对于那些具有管道分隔值的行,我想按如下方式扩展行:(这应该是最终的数据框)
Col1 Col2 Col3 Col4
AB 2i 2j 2y
AB 2i 2k 2y
CD 3j 3k 3x
CD 3j 3p 3x
CD 3j 3e 3x
So, the pipeline separated values have to be expanded into their own rows and the other field values have to be copied in. How to do that in pandas dataframe?因此,必须将管道分隔值扩展为它们自己的行,并且必须复制其他字段值。如何在 pandas dataframe 中做到这一点?
Use for pandas 0.25.0+ Series.str.split
with DataFrame.assign
for column filled of lists and then DataFrame.explode
, last for default index DataFrame.reset_index
with drop=True
: Use for pandas 0.25.0+
Series.str.split
with DataFrame.assign
for column filled of lists and then DataFrame.explode
, last for default index DataFrame.reset_index
with drop=True
:
df = df.assign(Col3 = df['Col3'].str.split('|')).explode('Col3').reset_index(drop=True)
print (df)
Col1 Col2 Col3 Col4
0 AB 2i 2j 2y
1 AB 2i 2k 2y
2 CD 3j 3k 3x
3 CD 3j 3p 3x
4 CD 3j 3e 3x
EDIT: If column name has space:编辑:如果列名有空格:
print (df)
Col1 Col2 my col Col4
0 AB 2i 2j|2k 2y
1 CD 3j 3k|3p|3e 3x
df['my col'] = df['my col'].str.split('|')
df = df.explode('my col').reset_index(drop=True)
print (df)
Col1 Col2 my col Col4
0 AB 2i 2j 2y
1 AB 2i 2k 2y
2 CD 3j 3k 3x
3 CD 3j 3p 3x
4 CD 3j 3e 3x
Solution for oldier versions:旧版本的解决方案:
c = df.columns
s = (df.pop('Col3')
.str.split('|', expand=True)
.stack()
.reset_index(drop=True, level=1)
.rename('Col3'))
df = df.join(s).reset_index(drop=True)[c]
print (df)
Col1 Col2 Col3 Col4
0 AB 2i 2j 2y
1 AB 2i 2k 2y
2 CD 3j 3k 3x
3 CD 3j 3p 3x
4 CD 3j 3e 3x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.