Transform data from PGN to data-friendly formats like dataframes.

pgn2df[source]

pgn2df(pgn)

Put the data from the PGN headers into a dataframe.

pgn = open("data/magnus.pgn")
df = pgn2df(pgn)
df.shape
(5, 19)
df.head()
Event Site Date Round White Black Result BlackElo BlackRatingDiff BlackTitle ECO Termination TimeControl UTCDate UTCTime Variant WhiteElo WhiteRatingDiff WhiteTitle
0 Rated Blitz game https://lichess.org/lAV0T0zl 2021.12.23 ? DrNykterstein may6enexttime 1-0 2974 -2 GM B20 Normal 180+0 2021.12.23 23:28:07 Standard 3212 +2 GM
1 Rated Blitz game https://lichess.org/Nfk9W01S 2021.12.23 ? may6enexttime DrNykterstein 0-1 3209 +3 GM E48 Time forfeit 180+0 2021.12.23 23:21:54 Standard 2977 -3 GM
2 Rated Blitz game https://lichess.org/mpxofZFJ 2021.12.23 ? DrNykterstein may6enexttime 1-0 2979 -2 GM B20 Time forfeit 180+0 2021.12.23 23:15:48 Standard 3207 +2 GM
3 Rated Blitz game https://lichess.org/hjSxkukT 2021.12.23 ? may6enexttime DrNykterstein 0-1 3204 +3 GM E48 Normal 180+0 2021.12.23 23:10:43 Standard 2982 -3 GM
4 Rated Blitz game https://lichess.org/5cgs1l6r 2021.12.23 ? DrNykterstein may6enexttime 1-0 2984 -2 GM B20 Normal 180+0 2021.12.23 23:07:55 Standard 3202 +2 GM
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 19 columns):
 #   Column           Non-Null Count  Dtype 
---  ------           --------------  ----- 
 0   Event            5 non-null      object
 1   Site             5 non-null      object
 2   Date             5 non-null      object
 3   Round            5 non-null      object
 4   White            5 non-null      object
 5   Black            5 non-null      object
 6   Result           5 non-null      object
 7   BlackElo         5 non-null      object
 8   BlackRatingDiff  5 non-null      object
 9   BlackTitle       5 non-null      object
 10  ECO              5 non-null      object
 11  Termination      5 non-null      object
 12  TimeControl      5 non-null      object
 13  UTCDate          5 non-null      object
 14  UTCTime          5 non-null      object
 15  Variant          5 non-null      object
 16  WhiteElo         5 non-null      object
 17  WhiteRatingDiff  5 non-null      object
 18  WhiteTitle       5 non-null      object
dtypes: object(19)
memory usage: 888.0+ bytes
assert df.shape == (5, 19)
df.to_csv("data/magnus.csv")

pgn2csv[source]

pgn2csv(in_pgn, out_csv)

Extract the data from a PGN file to a CSV.