'efficient way of computing a list with mean of values in another list
I need to compute a list with the mean values of another list. To be more precise, the input list have this form:
input_list =
['1.538075/42.507325',
'1.537967/42.507690',
'1.538292/42.507742',
'1.538399/42.507376',
'1.538075/42.507325']
And I need to compute a list with the mean of the values before and after the slash ("/"), like this result:
desired_output =
[1.5381616, 42.5074916]
I can obtain the desired_output correctly using this code:
desired_output = pd.Series(input_list)\
.apply(lambda r: pd.Series(r.split('/')))\
.astype(float)\
.mean()\
.tolist()
However, I have a very large number of input lists and the proposed code is somewhat slow, so I need to find a more efficient way to do it.
Any suggestions?
Solution 1:[1]
You don't really need pandas here, a simple list comprehension should work:
input_list = ['1.538075/42.507325',
'1.537967/42.507690',
'1.538292/42.507742',
'1.538399/42.507376',
'1.538075/42.507325']
from statistics import mean
out = [mean(map(float, x)) for x in zip(*(x.split('/') for x in input_list))]
output: [1.5381616, 42.5074916]
Or using numpy:
np.vstack([np.fromstring(s, sep='/') for s in input_list]).mean(0).tolist()
Solution 2:[2]
.apply
is the slow part, but luckily Pandas has the .str
accessor to vectorise string operations. This should be considerably faster:
desired_output = (pd.Series(input_list)
.str.split('/', expand=True)
.astype(float)
.mean()
.tolist())
Solution 3:[3]
Create a numpy array with dtype=float
, then calculate mean along axis=0
np.array([s.split('/') for s in input_list], dtype=float).mean(0)
array([ 1.5381616, 42.5074916])
Solution 4:[4]
Another way, using pandas and comprehensions -
pd.DataFrame([_.split('/') for _ in input_list]).astype(float).mean().to_list()
# [1.5381616, 42.5074916]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Elias Mi |
Solution 3 | Shubham Sharma |
Solution 4 | Mortz |