Python Forum
check how many times an item appears in list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: check how many times an item appears in list (/thread-14838.html)



check how many times an item appears in list - davidm - Dec-19-2018

Hi, I want to know how many times an item appears in a list.
If I have a list [1, 2, 2, 2] how would I check if 2 occurs 3 times.
Thanks for any help
David


RE: check how many times an item appears in list - ichabod801 - Dec-19-2018

list_name.count(2)


RE: check how many times an item appears in list - davidm - Dec-19-2018

Hi, thanks for this tried:
if list_name.count(2) = 3
Which gives invalid syntax on the "="
Any ideas?
Thanks
David


RE: check how many times an item appears in list - buran - Dec-19-2018

= is assignment, == is comparison. you need the latter

by the way - with closing tags, use forward slash, not back-slash


RE: check how many times an item appears in list - davidm - Dec-20-2018

Thanks Buran, works now!
You say about Using forward not back slash. I have a more serious problem I'm located in Israel and the text is all right aligned how do I change this to English left aligned
Thanks
David


RE: check how many times an item appears in list - buran - Dec-20-2018

As explained by @stranac in your other thread that is browser specific setting
For example for Firefox look at https://support.mozilla.org/bg/questions/1128107


RE: check how many times an item appears in list - davidm - Dec-20-2018

My browser - Chrome - is English ltr only in this forum the post text is rtl when i enter it, after posting it's ltr - very confusing.


RE: check how many times an item appears in list - davidm - Dec-29-2018

Hi, I'm still trying to get value_counts from multiple columns of a csv file this code:
import pandas as pd
df = pd.read_csv("lotto-0807-1218.csv")
print(df['Num 1'].value_counts().to_frame())
Prints the correct value counts for a single column, but how would I get value counts over multiple columns, if I try
print(df['Num 1', 'Num 2'].value_counts().to_frame())
I get:
Error:
KeyError: ('Num 1', 'Num 2')
I want the total for multiple columns not per column

Any help much appreciated.
Thanks
David

Here's the sample lotto-0807-1218.csv

Num 1,Num 2,Num 3,Num 4,Num 5,Num 6
15,21,23,27,36,37
8,11,13,18,25,31
1,3,7,9,15,22
7,8,25,31,32,35
3,5,8,18,19,29
2,12,14,18,22,25
3,5,16,18,21,26
6,16,23,24,30,33
9,20,25,27,34,37
16,18,20,22,24,25
8,9,13,14,27,29
2,7,9,19,24,25
4,8,13,20,27,29
6,7,11,17,22,29
11,17,19,25,27,28



RE: check how many times an item appears in list - davidm - Dec-29-2018

After Googling around since my last post I finally came up with this solution:
import pandas as pd
df = pd.read_csv("Lottery-numbers-only.csv")
print(pd.value_counts(df.values.flatten()))