Python Forum
Counting Criteria in Pandas Question - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Counting Criteria in Pandas Question (/thread-21425.html)



Counting Criteria in Pandas Question - Koenig - Sep-29-2019

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)



RE: Counting Criteria in Pandas Question - perfringo - Sep-30-2019

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