Home > database >  Dataframe. Split string from X to column value
Dataframe. Split string from X to column value

Time:04-06

I have a dataframe like this one: (comes from the Chess Game Dataset)

moves 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 O-O O-O d3 Ne8 5

I want to split the moves column (creating a new one for example) by the number of opening_moves indicated in the following column. Desired result:

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 O-O O-O d3 Ne8 3 e4 e5 Nf3

It sounds simple, but I didn't manage to find a similar post. This doesn't work:

split()[0:'number_of_opening_moves':3]

CodePudding user response:

Try list comprehension:

df['opening_moves'] = [x.split()[:int(i)] for x, i in zip(df['moves'], df['number_of_opening_moves'])]
  • Related