From .pgn to Dataframe (Pandas)

21 views
Skip to first unread message

Guerra

unread,
Oct 21, 2019, 1:59:28ā€ÆPM10/21/19
to PyChess People

Hi all, dear chess lovers!


I have a question I hope you could help me :)


I would like to create a data frame (in pandas module) with this data where the column titles are the words repeated in the brackets such as "Event" or "Site" and the data is the one in the quotation marks. Also, I would like to include the moves of course, which are not in quotation marks nor in brackets...


I have a single .pgnĀ of a large number of chess games. The games are contained in the file like this:

[Event"FIDE World Cup 2017"]
[Site "Tbilisi GEO"]
[Date "2017.09.05"]
[Round "1.1"]
[White "Carlsen, Magnus"]
[Black "Balogun, Oluwafemi"]
[Result "1-0"]
[WhiteTitle "GM"]
[BlackTitle "FM"]
[WhiteElo "2822"]
[BlackElo "2255"]
[ECO "B00"]
[Opening "King's pawn opening"]
[WhiteFideId "1503014"]
[BlackFideId "8501246"]
[EventDate "2017.09.03"]

1. e4 d6 2. d4 g6 3. Bc4 Nf6 4. Qe2 Nc6 5. Nf3 Bg7 6. O-O Bg4 7. c3 O-O         
8. h3 Bxf3 9. Qxf3 e5 10. Rd1 Qe8 11. d5 Ne7 12. Qe2 Nh5 13. Bb5 Qc8 
14. Na3 a6 15. Ba4 f5 16. Bc2 f4 17. Qg4 Qxg4 18. hxg4 Nf6 19. g5 Nd7 
20. Nc4 b6 21. b4 h6 22. gxh6 Bxh6 23. g4 Nf6 24. f3 Bg5 25. Kg2 Kg7 
26. a4 Bh4 27. Bd2 g5 28. Rh1 Ng6 29. Kf1 Rh8 30. Ke2 Bg3 31. a5 b5 32. 
Na3 Ne7 33. c4 c6 34. dxc6 Nxc6 35. Bc3 Rxh1 36. Rxh1 bxc4 37. Nxc4 Rb8 
38. Nxd6 Kg6 39. Nf5 1-0

[Event "FIDE World Cup 2017"]    
etc...

I would like to create a data frame with this data where the column titles are the words repeated in the brackets such as "Event" or "Site" and the data is the one in the quotation marks. Also, I would like to include the moves of course, which are not in quotation marks nor in brackets...

I think I should use pandas.read_fwf but I donĀ“t have any idea how to proceed with it. Could you please help me?

This is how I want to organize the data, in rows and columns

Event               Site    Date       Round White           Black  
FIDE World Cup 2017 Tbilisi 2017.09.05 1.1   Carlsen, Magnus Balogun, Oluwafemi

Thanks!!

TamƔs Bajusz

unread,
Oct 21, 2019, 2:08:07ā€ÆPM10/21/19
to PyChess People
Reply all
Reply to author
Forward
0 new messages