I have a dataframe that looks like this:
A
1 [67.0, 51.0, 23.0, 49.0, 3.0]
2 0
3 [595.0]
4 0
5 [446.0, 564.0, 402.0]
6 0
7 0
I would like to find the mean for each list ignoring the zeros. I want to get something like:
A Mean
1 [67.0, 51.0, 23.0, 49.0, 3.0] 38.6
2 0 0
3 [595.0] 595.0
4 0 0
5 [446.0, 564.0, 402.0] 470.7
6 0 0
7 0 0
I tried many possible solutions listed here and none of them worked. This is what I tried so far:
df['Mean'] = df.A.apply(lambda x: mean(x))
which gives me this error
TypeError: 'int' object is not iterable
Also this
df['Mean'] = df['A'].mean(axis=1)
ValueError: No axis named 1 for object type
Tried these as well with no luck:
a = np.array( df['A'].tolist())
a.mean(axis=1)
mean(d for d in a if d)
Is there something else I can try that would give me the expected outcome? Thanks for your help.
dtype()ofA??