Issue
I have a pandas Series like this, name place
:
0 [[[1,2], [3,4], [5,6], [7,8]]]
1 [[[3,2], [6,4], [7,6], [2,8]]]
2 nan
...
the elements of each row are the coordinates of the vertices of a box.
I would like to find the center of the box, keeping the nan
values as I want to concatenate this series to a dataframe and I need the same number of rows.
My initial idea was to write a function to calculate the mean:
def centroid(arr_val):
if arr_val:
center = np.mean(np.array(arr_val[0]), axis=0)
else:
center = arr_val
return center
and apply it to each element of the series:
place.apply(lambda x: centroid(x))
but I have some problem with the dimention of the list in the series as it's a list of list of lists...
What I would like to have is:
0 [4., 5.]
1 [4.5, 5. ]
2 nan
...
Any suggestion?
Solution
Your issue is not the dimensions of the lists, it is that you are not properly checking whether the input is nan
or not. Your test passes even when arr_val
is nan
:
arr_val = np.nan
if arr_val:
print('nan is truthy')
Prints nan is truthy
. See Why do "Not a Number" values equal True when cast as boolean in Python/Numpy?
Changing your test - given your data it's probably simplest just to check for a list - will resolve your problem:
def centroid(arr_val):
if isinstance(arr_val, list):
center = np.mean(np.array(arr_val[0]), axis=0)
else:
center = arr_val
return center
place = pd.Series([
[[[1,2], [3,4], [5,6], [7,8]]],
[[[3,2], [6,4], [7,6], [2,8]]],
np.nan
])
place.apply(centroid)
Output:
0 [4.0, 5.0]
1 [4.5, 5.0]
2 NaN
dtype: object
Answered By - Nick
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.