我有一個像這樣的資料框:(來自國際象棋游戲資料集)
| 移動 | number_of_opening_moves |
|---|---|
| e4 e5 Nf3 d6 d4 Nc6 d5 Nb4 a3 Na6 Nc3 Be7 b4 | 5 |
| e4 e5 Bc4 Nc6 Nf3 Nd4 d3 Nxf3 Qxf3 | 4 |
| e4 e6 d4 d5 e5 c5 c3 Nc6 Nf3 Qb6 Be3 Qxb2 | 2 |
| e4 e5 Nf3 Nc6 Bc4 Nf6 Nc3 Bc5 OO OO d3 Ne8 | 5 |
我想通過以下列中指示的 opening_moves 的數量來拆分移動列(例如創建一個新列)。期望的結果:
| 移動 | number_of_opening_moves | opening_moves |
|---|---|---|
| e4 e5 Nf3 d6 d4 Nc6 d5 Nb4 a3 Na6 Nc3 Be7 b4 | 5 | e4 e5 Nf3 d6 d4 |
| e4 e5 Bc4 Nc6 Nf3 Nd4 d3 Nxf3 Qxf3 | 4 | e4 e5 Bc4 Nc6 |
| e4 e6 d4 d5 e5 c5 c3 Nc6 Nf3 Qb6 Be3 Qxb2 | 2 | e4 e6 |
| e4 e5 Nf3 Nc6 Bc4 Nf6 Nc3 Bc5 OO OO d3 Ne8 | 3 | e4 e5 Nf3 |
聽起來很簡單,但我沒有找到類似的帖子。這不起作用:
split()[0:'number_of_opening_moves':3]
uj5u.com熱心網友回復:
嘗試串列理解:
df['opening_moves'] = [x.split()[:int(i)] for x, i in zip(df['moves'], df['number_of_opening_moves'])]
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/455749.html
