I had created a python package called pgnhelper with documentation which can be used to see what you would like to find.
Do not use a big pgn file as it will take more time. This is more useful for examining stats of a tournament. 5 or so tournament pgn files can be combined. Player names should be consistent.
Typical setup for windows 10
Intall python 3.7 or later from https://www.python.org/downloads/
Install pgnhelper from command line.
pip install pgnhelper -U
- Save the code below to ruylopez.py.
3.1 Download candidates games from https://theweekinchess.com/assets/files/pgn/wchcand22.pgn
3.2. Place the pgn file and ruylopez.py in the same directory.
- Execute ruylopez.py from command line like an example below.
PS F:\pgnhelpter_test> python ruylopez.py
The ruylopez.py and wchcand22.pgn are in F:\pgnhelpter_test path where pgnhelpter_test
is the folder or directory name.
Code
ruylopez.py
"""Show player names and number of games on the given opening name.
requirements:
pip install pgnhelper -U
"""
import pgnhelper
import pandas as pd
# opening_name = 'Nimzo-Indian'
opening_name = 'Ruy Lopez'
pgnfn = 'wchcand22.pgn'
# https://theweekinchess.com/assets/files/pgn/wchcand22.pgn
df, players, israting = pgnhelper.record.get_pgn_data(pgnfn)
# print(df)
# df.to_csv("sample.csv", index=False)
openings = df.Opening.unique()
for o in openings:
print(o)
data = {}
for p in players:
data1 = {}
for o in openings:
dfw = df.loc[(df.Opening == o) & (df.White == p)]
dfb = df.loc[(df.Opening == o) & (df.Black == p)]
wcnt = len(dfw)
bcnt = len(dfb)
total = wcnt + bcnt
data1.update({o: {'w': wcnt, 'b': bcnt, 'total': total}})
data.update({p: {'data': data1}})
mydata = []
for p in players:
mydata.append([opening_name, p,
data[p]['data'][opening_name]['w'],
data[p]['data'][opening_name]['b'],
data[p]['data'][opening_name]['total']])
mydf = pd.DataFrame(
mydata,
columns=['Opening', 'Player', 'Wgames', 'Bgames', 'Total'])
mydf = mydf.sort_values(by=['Total', 'Wgames'], ascending=[False, False])
print(mydf)
mydf.to_csv("ruylopez.csv", index=False) # Save to csv file
Output
Sicilian defence
English opening
Ruy Lopez
QGD
Sicilian, Chekhover variation
Giuoco Pianissimo
Gruenfeld
Sicilian
Catalan
Nimzo-Indian
Catalan opening
Petrov
Reti
Four knights game
Four knights
English
Giuoco Piano
QGD semi-Slav
Opening Player Wgames Bgames Total
2 Ruy Lopez Nakamura, Hikaru 2 4 6
7 Ruy Lopez Rapport, Richard 4 1 5
0 Ruy Lopez Caruana, Fabiano 3 2 5
1 Ruy Lopez Radjabov, Teimour 1 3 4
3 Ruy Lopez Firouzja, Alireza 1 1 2
6 Ruy Lopez Ding, Liren 0 2 2
4 Ruy Lopez Nepomniachtchi, Ian 1 0 1
5 Ruy Lopez Duda, Jan-Krzysztof 1 0 1
The code can be revised to show a column of score percentage. Can also be revised to show by ECO code.
Opening names in the game are printed in console due to:
openings = df.Opening.unique()
for o in openings:
print(o)
Results are printed in console due to:
print(mydf)
csv file will be saved due to:
mydf.to_csv("ruylopez.csv", index=False) # Save to csv file
You can change the opening name in the code by changing below code:
opening_name = 'Ruy Lopez'
You can use different pgn file but be sure the game has Opening tag like below.
[Event "FIDE Candidates 2022"]
[Site "Madrid ESP"]
[Date "2022.06.17"]
[Round "1.3"]
[White "Caruana, Fabiano"]
[Black "Nakamura, Hikaru"]
[Result "1-0"]
[WhiteTitle "GM"]
[BlackTitle "GM"]
[WhiteElo "2783"]
[BlackElo "2760"]
[ECO "C65"]
[Opening "Ruy Lopez"]
[Variation "Berlin defence"]
[WhiteFideId "2020009"]
[BlackFideId "2016192"]
[EventDate "2022.06.17"]
1. e4 e5 2. Nf3 Nc6 ...
There must be Opening
tag.
[Opening "Ruy Lopez"]
Add eco
If the game in your pgn file has no Opening tag, you can use pgnhelper to add a tag.
Example from command line:
pgnhelper addeco --inpgnfn candidates_zurich_1953.pgn --outpgnfn eco_candidates_zurich_1953.pgn --inecopgnfn eco.pgn
See also the guide at https://pgnhelper.readthedocs.io/en/latest/usage.html.
The file eco.pgn
is needed, you can download it from github.
For example you can download pgn files from weekinchess, or from pgnmentor.