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) = 3Which 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 latterby 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: I want the total for multiple columns not per columnAny help much appreciated. Thanks David Here's the sample lotto-0807-1218.csv
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())) |