1 回答
TA贡献1803条经验 获得超6个赞
您可以在预处理中使用maskwith difference,然后使用此解决方案:
c1 = df.columns.difference(['Type','Name'])
c2 = df.columns.difference(['Type','Color'])
df[c1] = df[c1].mask(df['Type'] == 'System', np.nan)
df[c2] = df[c2].mask(df['Type'] == 'Hardware', np.nan)
cols = df.columns
df1 = (df.join(df.pop('Text').str.split('\n', expand=True)
.stack()
.reset_index(level=1, drop=True)
.rename('Text'))
).reset_index(drop=True).reindex(columns=cols)
print (df1)
Type Text Name ID Color
0 System NaN Gary NaN NaN
1 System NaN Mary NaN NaN
2 Hardware NaN NaN NaN White
3 Software ca Perry 56414.0 Green
4 Software maca Perry 56414.0 Green
5 Software stream Perry 56414.0 Green
6 Software phase Perry 56414.0 Green
7 Software Perry 56414.0 Green
8 Software aca Jimmy 548.0 Blue
9 Software mac Jimmy 548.0 Blue
10 Software stream Jimmy 548.0 Blue
11 Software phase Jimmy 548.0 Blue
12 Software Jimmy 548.0 Blue
13 System NaN Marc NaN NaN
14 System NaN John NaN NaN
15 Hardware NaN NaN NaN Gray
16 Hardware NaN NaN NaN Azure
17 System NaN Larry NaN NaN
18 Software aca James 6568.0 Magenta
19 Software maca James 6568.0 Magenta
20 Software st James 6568.0 Magenta
21 Software phase James 6568.0 Magenta
22 Software James 6568.0 Magenta
23 System NaN Kevin NaN NaN
添加回答
举报