Python Forum
Counting Criteria in Pandas Question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting Criteria in Pandas Question
#1
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)
Reply
#2
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pandas df inside a df question mbaker_wv 4 1,152 Dec-25-2022, 01:11 AM
Last Post: mbaker_wv
  Pandas usecols question rsearing 1 1,218 Aug-20-2022, 10:10 PM
Last Post: jefsummers
  Simple pandas question mcva 4 2,607 Dec-17-2021, 04:47 PM
Last Post: mcva
  Pandas question new2datasci 0 1,928 Jan-10-2021, 01:29 AM
Last Post: new2datasci
  Pandas merge question smw10c 2 5,675 Jul-02-2020, 06:56 PM
Last Post: hussainmujtaba
  Function question using Pandas smw10c 7 7,012 Feb-12-2019, 06:52 PM
Last Post: Nathandsn
  Simple pandas dataframe question popohoma 1 3,496 Jan-03-2019, 05:00 PM
Last Post: ashlardev
  question on pandas datareader kit12_31 3 9,160 Feb-05-2018, 11:55 PM
Last Post: snippsat
  Newbie question on how to use pandas.rolling_mean zydjohn 5 14,167 Dec-09-2017, 08:42 PM
Last Post: j.crater
  Pandas question takaa 3 3,779 Dec-05-2017, 01:03 PM
Last Post: takaa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020