2

Say I have a list:

mylist = ['a','b','c']

and a Pandas dataframe (df) that has a column named "rating". How can I get the count for number of occurrence of a rating while iterating my list? For example, here is what I need:

for item in myList
   # Do a bunch of stuff in here that takes a long time
   # want to do print statement below to show progress
   # print df['rating'].value_counts().a <- I can do this, 
   #     but want to use variable 'item'
   # print df['rating'].value_counts().item <- Or something like this

I know I can get counts for all distinct values of 'rating', but that is not what I am after.

3 Answers 3

1

If you must do it this way, you can use .loc to filter the df prior to getting the size of the resulting df.

mylist = ['a','b','c']
df = pd.DataFrame({'rating':['a','a','b','c','c','c','d','e','f']})


for item in mylist:
    print(item, df.loc[df['rating']==item].size)

Output

a 2
b 1
c 3
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this is what I needed. There is a lot going on behind the hood that I did not include, and my print statement is sort of a progress capture, since each iteration can take a long time. Because of that I did not want to just get everything after my dataframe is complete (I am adding records to it each pass).
1

Instead of thinking about this problem as one of going "from the list to the Dataframe" it might be easiest to flip it around:

mylist = ['a','b','c']
df = pd.DataFrame({'rating':['a','a','b','c','c','c','d','e','f']})

ValueCounts = df['rating'].value_counts()
ValueCounts[ValueCounts.index.isin(mylist)]

Output:

c    3
a    2
b    1
Name: rating, dtype: int64

Comments

1

You don't even need a for loop, just do:

df['rating'].value_counts()[mylist]

Or to make it a dictionary:

df['rating'].value_counts()[['a', 'b', 'c']].to_dict()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.