在我的資料框中,我有一個名為 pca 的列,它有 2 個值。

我想將它們分成 2 列。
我的想法是,我使用 str.split 將它們分開,然后洗掉每列中的方括號。
我嘗試使用
getClusterReview[['pca_A', 'pca_B']] = getClusterReview['pca'].str.split(',', expand=True)
但是,我一直堅持拆分,因為我收到一條錯誤訊息,指出列的長度必須相同。
uj5u.com熱心網友回復:
首先使用將作為拆分列的值創建串列,然后將它們添加到 df,并根據需要洗掉舊列:
pca1 = []
pca2 = []
for element in getClusterReview["pca"]:
pca1.append(element[0])
pca2.append(element[1])
getClusterReview["pca1"] = pca1
getClusterReview["pca2"] = pca2
getClusterReview.drop("pca", axis=1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/350800.html
