2

I want to count the frequency of rows from a column and with the frequency greater than 3, I want to populate an array with those rows.

For example:

402       Sony
403       Sony
404       Sony
405        ZTE
406        ZTE
407        ZTE
408        ZTE
409        ZTE

Desired array:

[
"ZTE",
"ZTE",
"ZTE",
"ZTE",
"ZTE"]

1 Answer 1

1

Use collections.Counter() to get the occurance frequency. So assuming the data you provided comes from the file test.txt:

import collections
itemList = []
with open("test.txt", 'r') as f:
    for line in f:
        itemList.append(line.split()[1])
result = collections.Counter(itemList)
print(result)
#get the n most repeated values in a list of tuples:
n = 1
print(result.most_common(n))

Will input

402       Sony
403       Sony
404       Sony
405        ZTE
406        ZTE
407        ZTE
408        ZTE
409        ZTE

And output:

Counter({'ZTE': 5, 'Sony': 3})
[('ZTE', 5)]
Sign up to request clarification or add additional context in comments.

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.