How I can count the total elements in a dataframe, including the subset, and put the result in the new column?
import pandas as pd
x = pd.Series([[1, (2,5,6)], [2, (3,4)], [3, 4], [(5,6), (7,8,9)]], \
index=range(1, len(x)+1))
df = pd.DataFrame({'A': x})
I tried with the following code but it gives 2 in each of row:
df['Length'] = df['A'].apply(len)
print(df)
A Length
1 [1, (2, 5, 6)] 2
2 [2, (3, 4)] 2
3 [3, 4] 2
4 [(5, 6), (7, 8, 9)] 2
However, what I want to get is as follow:
A Length
1 [1, (2, 5, 6)] 4
2 [2, (3, 4)] 3
3 [3, 4] 2
4 [(5, 6), (7, 8, 9)] 5
thanks