更新答案h5>
list_Crashes = ['Startup', 'Crash in A', 'Shutdown', 'Crash in B', 'Crash in C', 'Startup', 'Crash in D',
'Startup', 'Crash in E', 'Crash in F', 'Crash in G', 'Shutdown', 'Crash in X', 'Crash in Y', 'Crash in Z']
我有一个包含2列的表。代码将检查list的前一个元素并查找(Startup/Shutdown):例如:如果在启动后发生崩溃;State列将在Crash前填充Startup,如下表所示:
<表类> 崩溃国家 tbody><<tr>崩溃在 启动 B崩溃 关机 C程序崩溃 关机 崩溃在D 启动 E Startup F Startup 崩溃在G 启动 Crash in X Shutdown 在Y中崩溃 关机 崩溃 关机 表类>
如果是这样,可以使用生成器:
def gen(lst):
last_non_crash = ''
for x in lst:
if not x.startswith('Crash in'):
last_non_crash = x
else:
yield [x, last_non_crash]
pd.DataFrame(gen(list_Crashes), columns=['Crashes', 'State'])
输出:
Crashes State
0 Crash in A Startup
1 Crash in B Shutdown
2 Crash in C Shutdown
3 Crash in D Startup
4 Crash in E Startup
5 Crash in F Startup
6 Crash in G Startup
7 Crash in X Shutdown
8 Crash in Y Shutdown
9 Crash in Z Shutdown
输入:
list_Crashes = ['Startup', 'Crash in A', 'Shutdown', 'Crash in B', 'Crash in C', 'Startup', 'Crash in D',
'Startup', 'Crash in E', 'Crash in F', 'Crash in G', 'Shutdown', 'Crash in X', 'Crash in Y', 'Crash in Z']
更新答案h5>Crashes State
0 Crash in A Startup
1 Crash in B Startup
2 Crash in C Startup
3 Crash in D Startup
4 Crash in E Startup
5 Crash in Y Shutdown
Crashes State
0 Crash in A Startup
1 Crash in B Startup
2 Crash in C Startup
3 Crash in D Startup
4 Crash in E Startup
5 Crash in Y Shutdown
def function1(x):
return ' '.join(x[x.index('Crash'):x.index('Crash')+3]) if 'Crash' in x else ''
col2=df1.col2.str.split(' ')
Startup=col2.map(lambda x:'Startup' in x)
Shutdown=col2.map(lambda x:'Shutdown' in x)
Crash=np.where(Startup|Shutdown,False,col2.map(function1))
df1.assign(Crash=Crash).assign(State=np.select([Startup,Shutdown],['Startup','Shutdown'],None)).ffill()
.loc[Crash!=False]
:
Crashes State
0 Crash in A Startup
1 Crash in B Startup
2 Crash in C Startup
3 Crash in D Startup
4 Crash in E Startup
5 Crash in Y Shutdown