Find Average and Minimum Value from Array Object - Python - python-3.x

I have a data like below,
resultFromCalculation = [{'value40': {'A': 3.1, 'B': 5.62, 'C': 5.99, 'D': 5.06, 'E': 5.09}},
{'value50': {'A': 2.95, 'B': 5.21, 'C': 5.41, 'D': 4.64, 'E': 4.5}},
{'value60': {'A': 2.35, 'B': 4.8, 'C': 4.83, 'D': 4.08, 'E': 3.62}},
{'value70': {'A': 2.95, 'B': 5.21, 'C': 5.41, 'D': 4.64, 'E': 4.5}}]
I want to find average for A to E values for each list. Like,
avgValues = [{'value40':4.97},{'value50':4.41},{'value60':3.99},{'value70':3.99}]
From the above OP I need to find out which one is first least value than others.
FinalResultIs = value60

Using Pandas:
>>> pd.concat([pd.DataFrame(d) for d in resultFromCalculation], axis=1).mean()
value40 4.972
value50 4.542
value60 3.936
value70 4.542
dtype: float64
>>> pd.concat([pd.DataFrame(d) for d in resultFromCalculation], axis=1).mean().argmin()
'value60'
Using simple list comprehension, you can use
avgValues = [{list(d.keys())[0]: sum(list(d.values())[0].values()) / len(list(d.values())[0].values())} for d in resultFromCalculation]
>>> avgValues
[{'value40': 4.9719999999999995},
{'value50': 4.542},
{'value60': 3.936000000000001},
{'value70': 4.542}]
To find the minimum:
>>> min(avgValues, key=lambda e: list(e.values())[0])
{'value60': 3.936000000000001}

Use:
L = [pd.DataFrame(x).mean().to_dict() for x in resultFromCaluclation]
print (L)
[{'value40': 4.9719999999999995}, {'value50': 4.542}, {'value60': 3.936000000000001}, {'value70': 4.542}]

Related

How can we include only specific json fields for comparision in deepdiff pyhton?

Here, we have 2 JSON, I want to include only 'id' field for comparison, rest of the fields should be ignored.
j1 = {'MyList': [{'a': 1, 'b': 2, 'c': [{'id': '1'}]},
{'a': 1, 'b': 2, 'c': [{'id': '2'}]}], "j": "2222"}
j2 = {'MyList': [{'a': 1, 'b': 2, 'c': [{'id': '4'}]},
{'a': 1, 'b': 2, 'c': [{'id': '7'}]}], "j": "7777"}
Please suggest, How we can achieve this using DeepDiff.

Strange behavior by list of dictionaries [duplicate]

This question already has answers here:
How to copy a dictionary and only edit the copy
(23 answers)
Closed 1 year ago.
I have a list of dictionaries as follows:
a = [{'a':1, 'b':2, 'c':3}, {'d':4, 'e':5, 'f':6}]
Now I want another list b to have same contents as a but with one (key,value) pair extra. So I do it as:
b = a.copy()
for item in b:
item['x'] = 6
But now both the lists a and b have 'x': 6 sitting in them.
>>> b
[{'a': 1, 'b': 2, 'c': 3, 'x': 6}, {'d': 4, 'e': 5, 'f': 6, 'x': 6}]
>>> a
[{'a': 1, 'b': 2, 'c': 3, 'x': 6}, {'d': 4, 'e': 5, 'f': 6, 'x': 6}]
I also tried this:
c = a[:]
for item in c:
item['q'] = 12
And now all the three lists have 'q': 12.
>>> c
[{'a': 1, 'b': 2, 'c': 3, 'x': 6, 'q': 12}, {'d': 4, 'e': 5, 'f': 6, 'x': 6, 'q': 12}]
>>> b
[{'a': 1, 'b': 2, 'c': 3, 'x': 6, 'q': 12}, {'d': 4, 'e': 5, 'f': 6, 'x': 6, 'q': 12}]
>>> a
[{'a': 1, 'b': 2, 'c': 3, 'x': 6, 'q': 12}, {'d': 4, 'e': 5, 'f': 6, 'x': 6, 'q': 12}]
I can't understand how is this working. This would have been acceptable if I had done b = a. But why for b = a.copy() and c = a[:].
Thanks in advance:)
To copy a dictionary and copy all referenced objects use the deepcopy() function from the copy module instead of dict's method copy().
import copy
a = [{'a':1, 'b':2, 'c':3}, {'d':4, 'e':5, 'f':6}]
b = copy.deepcopy(d)
You can use #maziyank's solution. The explanation is that except copy.deepcopy() all the methods are just pointing one variable to the previous variable.
Thus any change to any of them will transcend to all the variables that point to the same variable.

How to calculate count of occurence of each value from a list column of Pandas efficiently?

I have a Pandas data frame, which looks like the following:
df =
col1
['a', 'b']
['d', 'c', 'a']
['b', 'f', 'a']
col1 is a list column, which contains strings. I want to calculate value counts of each element, which may occur in any of the lists in any row. Expected output is a dictionary, of counts of each value
Expected Output
df_dict = {'a': 3, 'b': 2, 'c': 1, 'd': 1, 'f': 1}
How to do this efficiently in 1 line preferably to make the code clean. Sorry, if it has been answered before.
With explode and value_counts:
df['col1'].explode().value_counts().to_dict()
Output:
{'a': 3, 'b': 2, 'd': 1, 'f': 1, 'c': 1}

How to extend values in dictionaries using key in Python

How do I extend the values in a dictionary from a list of dictionaries using the keys as the main constraint, say:
d = {'a': (), 'b': 0, 'c': "", d: ""}
l = [ {'a': (23, 48), 'b': 34, 'c': "fame", d: "who"},
{'a': (94, 29), 'b': 3, 'c': "house", d: "cats"},
{'a': (23, 12), 'b': 93, 'c': "imap", d: "stack"},
]
to give
d = {'a': [(23, 48), (94,29), 23,12], 'b': [34, 3, 94],
'c': ["fame", "house", "imap"], 'd': ['who', 'cats', 'stack'] }
code used
for i in l:
d["a"].extend(i.get('a')),
d["b"].extend(i.get('b')),
d["c"].extend(i.get('c')),
d['d'].extend(i.get('d'))
You should initialize d as an empty dict instead, so that you can iterate through l and the key-value pairs to keep appending the values to the sub-list of d at the given keys:
l = [
{'a': (23, 48), 'b': 34, 'c': "fame", 'd': "who"},
{'a': (94, 29), 'b': 3, 'c': "house", 'd': "cats"},
{'a': (23, 12), 'b': 93, 'c': "imap", 'd': "stack"},
]
d = {}
for s in l:
for k, v in s.items():
d.setdefault(k, []).append(v)
d becomes:
{'a': [(23, 48), (94, 29), (23, 12)],
'b': [34, 3, 93],
'c': ['fame', 'house', 'imap'],
'd': ['who', 'cats', 'stack']}
If the sub-dicts in l may contain other keys, you can instead initialize d as a dict of empty lists under the desired keys:
l = [
{'a': (23, 48), 'b': 34, 'c': "fame", 'd': "who"},
{'a': (94, 29), 'b': 3, 'c': "house", 'd': "cats"},
{'a': (23, 12), 'b': 93, 'c': "imap", 'd': "stack"},
{'e': 'choices'}
]
d = {k: [] for k in ('a', 'b', 'c', 'd')}
for s in l:
for k in d:
d[k].append(s.get(k))
in which case d becomes:
{'a': [(23, 48), (94, 29), (23, 12), None],
'b': [34, 3, 93, None],
'c': ['fame', 'house', 'imap', None],
'd': ['who', 'cats', 'stack', None]}
You can use defaultdict as follow since the default value is an empty list (https://docs.python.org/2/library/collections.html#collections.defaultdict)
import collections
d = collections.defaultdict(list)
keys = ['a', 'b', 'c', 'd']
for i in l:
for k in keys:
d[k].append(i[k])
print(d)
Best regard

Why do I get TypeError when trying to do dot product?

If I have one integer and multiply it by each integer in a container (tuple) and add them together -- similar to a dot product -- I get the right answer. When I convert them to floats, I get a TypeError:
TypeError: can't multiply sequence by non-int of type 'float'
sig = {'a': 1.0, 'b': 2.0, 'c': 3.0}
exp = {'a': (1.0,2.0,3.0), 'b': (1.0,2.0,3.0), 'c': (1.0,2.0,3.0)}
man_dot = {'a': 1*1+1*2+1*3, 'b': 2*1+2*2+2*3, 'c': 3*1+3*2+3*3}
weighted_dict = {}
for s in sig:
print("this is s:\n{}".format(s))
for e in exp:
print("this is e:\n{}".format(e))
weighted_dict[s] = sum(sig[s] * exp[e])
# weighted_dict should be equivalent to man_dot
# weighted_dict should be {'a': 6, 'c': 18, 'b': 12}
This script must handle operation with floats, so how can I modify it to do so? Why does this happen? Is there a better of of doing this with some math-oriented library?
Your problem is that you are trying to multiply (1.0, 2.0, 3.0) by 1.0, which gives the aforementioned error. Try the following:
sig = {'a': 1.0, 'b': 2.0, 'c': 3.0}
exp = {'a': (1.0,2.0,3.0), 'b': (1.0,2.0,3.0), 'c': (1.0,2.0,3.0)}
man_dot = {'a': 1*1+1*2+1*3, 'b': 2*1+2*2+2*3, 'c': 3*1+3*2+3*3}
weighted_dict = {}
for s in sig:
print("this is s:\n{}".format(s))
for e in exp:
print("this is e:\n{}".format(e))
weighted_dict[s] = sum([sig[s] * item for item in exp[e]])
>>> weighted_dict
{'c': 18.0, 'a': 6.0, 'b': 12.0}
>>>

Resources