我有一個如下所示的資料框: 原始資料
index string
0 a,b,c,d,e,f
1 a,b,c,d,e,f
2 a,(I,j,k),c,d,e,f
我想成為: 成為資料
index col1 col2 col3 col4 col5 col6
0 a b c d e f
1 a b c d e f
2 a (I,j,k) c d e f
uj5u.com熱心網友回復:
您可以拆分不在括號內的逗號。然后將結果轉換為 DataFrame 并分配給df列:
df[['col {}'.format(i) for i in range(1,7)]] = df['string'].str.split(r",\s*(?![^()]*\))").apply(pd.Series)
輸出:
index string col 1 col 2 col 3 col 4 col 5 col 6
0 0 a,b,c,d,e,f a b c d e f
1 1 a,b,c,d,e,f a b c d e f
2 2 a,(I,j,k),c,d,e,f a (I,j,k) c d e f
uj5u.com熱心網友回復:
試試這個 :
df = df['string'].str.split(r",\s*(?![^()]*\))", expand= True)
df.columns = ['col1','col2','col3','col4','col5','col6']
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/383790.html
上一篇:根據列中是否存在特定子字串創建列
