当系统出现在“类型”列中时,我想删除该行中的所有值,“名称”列中的值除外。当硬件出现在“类型”列中时,我想删除该行中除“颜色”列中的值之外的所有值。之后,我想将“文本”列中非空的所有单元格拆分为多行,并保留该列中为空的行。
这是我拥有的数据框:
df
Type Text Name ID Color
System aca\nmaca\nstream\nphase\n Gary 123 Red
System aca\nmaca\nstream\nphase\n Mary 3254 Yellow
Hardware a\nmaca\nstream\nphase\n Jerry 158 White
Software ca\nmaca\nstream\nphase\n Perry 56414 Green
Software aca\nmac\nstream\nphase\n Jimmy 548 Blue
System aca\nmaca\nstream\nphase\n Marc 5658 Black
System aca\nmaca\nstram\npha\n John 867 Pink
Hardware aca\nma\nstream\nphase\n Sam 665 Gray
Hardware aca\nmaca\nstream\nphase\n Jury 5784 Azure
System aca\nmaca\nstream\nphase\n Larry 5589 Fawn
Software aca\nmaca\nst\nphase\n James 6568 Magenta
System aca\nmaca\nstream\nph\n Kevin 568 Cyan
这是所需的结果:
Type Text Name ID Color
System Gary
System Mary
Hardware White
Software ca Perry 56414 Green
Software maca Perry 56414 Green
Software stream Perry 56414 Green
Software phase Perry 56414 Green
对于将单元格拆分为多行,我尝试了此功能:
def SepInRows(df, c):
s = df[c].str.split('\n', expand=True).stack()
i = s.index.get_level_values(0)
df2 = df.loc[i].copy()
df2[c] = s.values
return df2
但它会在“文本”列中删除具有空值的行,这不是我想要的。
如何解决这个问题?
慕码人8056858
相关分类