用Python检查列表中的前一个元素,并根据前一个元素在Pandas的新列中存储一个值


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启动EStartupFStartup崩溃在G启动Crash in XShutdown在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
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

相关内容

  • 没有找到相关文章

最新更新