I have the following data structure.
basket_series = df_train["basket"].head(10)
type(basket_series)
Output: pandas.core.series.Series
0 [3]
1 [5, 3, 0, 3]
2 [3, 3, 1, 4]
3 [2]
4 [4, 4, 4, 4]
5 [4, 3, 4, 4]
6 [3, 4]
7 [4, 4, 1, 4, 4]
8 [1, 5, 2, 2]
9 [5, 5, 0]
I want to know how many numbers are per "list" -> but I think that "the list" is only interpreted as a string. My approaches were:
basket_series.size()
output: 10
for x in basket_series:
print(len(x))
output: 3 12 12 3 12 12 6 15 12 9
Which seems to be the same as
basket_series.str.len()
for x in basket_series:
print(len(list(x)))
So the problem is that it is seen as a string? Do you have any ideas?
I guess the part of the answers are already covered in the comments but here is the code for completeness-
import pandas as pd
from ast import literal_eval
l = ['[3]',
'[5, 3, 0, 3]',
'[3, 3, 1, 4]',
'[2]',
'[4, 4, 4, 4]',
'[4, 3, 4, 4]',
'[3, 4]',
'[4, 4, 1, 4, 4]',
'[1, 5, 2, 2]',
'[5, 5, 0]']
s = pd.Series(l)
print(s.map(literal_eval).apply(len))
0 1
1 4
2 4
3 1
4 4
5 4
6 2
7 5
8 4
9 3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.