我有以下字符串列。
Str1 Str2 Str3
OK I Go
Yes Hm
Fine I see
我想将它们合并为一个列表变量
AllStr
["ok", "I", "Go"]
["Yes", "Hm"]
["Fine", "I see"]
我尝试了不同的方法,但我接近但不太正确:
df_manual_label['AllStr'] = df_manual_label[['Str1', 'Str2', 'Str3']].apply(lambda x: ', '.join(x.astype(str)), axis=1)
你可以做 stack
df['New']=df.replace('',np.nan).stack().groupby(level=0).apply(list)
df
Out[1666]:
Str1 Str2 Str3 New
0 OK I Go [OK, I, Go]
1 Yes Hm [Yes, Hm]
2 Fine I see [Fine, I see]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句