我有一個字串串列:
data = ['col1:abc col2:def col3:ghi',
'col4:123 col2:qwe col10:xyz',
'col3:asd']
我想將其轉換為資料框,其中串列中的每個字串都是資料框中的一行,如下所示:
desired_out = pd.DataFrame({'col1': ['abc', np.nan, np.nan],
'col2': ['def', 'qwe', np.nan],
'col3': ['ghi', np.nan, 'asd'],
'col4': [np.nan, '123', np.nan],
'col10': [np.nan, 'xyz', np.nan]})

uj5u.com熱心網友回復:
使用嵌套串列理解將拆分值轉換為字典:
df = pd.DataFrame([dict([y.split(':') for y in x.split()]) for x in data])
print (df)
col1 col2 col3 col4 col10
0 abc def ghi NaN NaN
1 NaN qwe NaN 123 xyz
2 NaN NaN asd NaN NaN
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/533371.html
上一篇:如何更新僅存盤更改的記錄?
