Python Forum

Full Version: Counting Criteria in Pandas Question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
Not sure where I'm going wrong at. Any help would be appreciated.

I have a csv file that has win/loss/draw data. Quick brief of the csv file:
3 comma separated columns. Columns are "Side", "Result" and "Year" with data below.

I'm trying to get the data from the csv file to pandas in python and to count the number of times "Win" appears but I'm getting a Key Error: 'Win' when I run the below script. Confirmed "Win" is in the csv file.


from pandas import DataFrame, read_csv
import matplotlib.pyplot as plt
import pandas as pd 

df = pd.read_csv('test.csv')
df[df["Result"]==1].count()["Win"]
print(df)
Is there any particular reason to import ths way:

from pandas import DataFrame, read_csv
import pandas as pd
You could use .value_conts() for counting values:

>>> df = pd.DataFrame({'numbers': range(1, 6), 'letters': [*'abaca']})
>>> df
   numbers letters
0        1       a
1        2       b
2        3       a
3        4       c
4        5       a
>>> df.letters.value_counts()['a']
3