Reputation: 25
I'm trying to add up the number of equal values in a list. The list looks like this:
list = [["APP", "X", "v3", "CN_L", "2"],
["APP2", "X", "v3", "CN_M", "2"],
["APP3", "Z", "v3", "CN_L", "2"],
["APP2", "Z", "v3", "CN_M", "2"]]
etc.
I am mainly concerned with the number of times the 4th field is found.
I am not very experienced in Python. I had already found something about Counter and I tried something with it.
from collections import Counter
list = [["APP", "X", "v3", "CN_L", "2"],
["APP2", "X", "v3", "CN_M", "2"],
["APP3", "Z", "v3", "CN_L", "2"],
["APP2", "Z", "v3", "CN_M", "2"]]
distinct_list=(Counter(list).keys())
Without for loop I get nothing from this code, and get an unhashable type back. Who can push me in the right direction?
Upvotes: -1
Views: 111
Reputation: 24
I would put the data into a pandas Dataframe like this:
import pandas as pd
df = pd.DataFrame(
[["APP", "X", "v3", "CN_L", "2"],
["APP2", "X", "v3", "CN_M", "2"],
["APP3", "Z", "v3", "CN_L", "2"],
["APP2", "Z", "v3", "CN_M", "2"]]
)
df[4].value_counts()
->
2 4
Name: 4, dtype: int64
It will return you a pandas Series which is basically working like a dict so you can do:
x = df[4].value_counts()
x["2"] --> 4
Upvotes: 0
Reputation: 2750
from collections import Counter
new_list = [["APP", "X", "v3", "CN_L", "2"],
["APP2", "X", "v3", "CN_M", "2"],
["APP3", "Z", "v3", "CN_L", "2"],
["APP2", "Z", "v3", "CN_M", "2"]]
#import numpy library
import numpy as np
#convert the list into a numpy array
arr=np.array(new_list)
#take the 4 th column and then apply the counter
result=Counter(arr[:,4])
Upvotes: 0
Reputation: 2900
Use [l[3] for l in my_list]
to get the elements at index 3 (4th elements), then simply calling Counter
on your list will give you the unique elements and their count.
from collections import Counter
my_list = [["APP", "X", "v3", "CN_L", "2"],
["APP2", "X", "v3", "CN_M", "2"],
["APP3", "Z", "v3", "CN_L", "2"],
["APP2", "Z", "v3", "CN_M", "2"]]
forth_elts = [l[3] for l in my_list]
print(Counter(forth_elts))
>>> Counter({'CN_M': 2, 'CN_L': 2})
And please avoid using keywords and other words such as "str", or "list" to name your variables.
Upvotes: 1