计算平均数嵌套系列

时间:2020-07-20 14:06:25

标签: python pandas data-science

我正在尝试找到burrito_orders["choice_description"]的长度。

burrito_orders= df[df['item_name'].str.contains('Burrito|burrito')]
         
burrito_orders["length"] = burrito_orders["choice_description"].size
    
burrito_orders

很明显,我得到的行数

{'order_id': {7: 4},
 'quantity': {7: 1},
 'item_name': {7: 'Steak Burrito'},
 'choice_description': {7: '[Tomatillo Red Chili Salsa, [Fajita Vegetables, Black Beans, Pinto Beans, Cheese, Sour Cream, Guacamole, Lettuce]]'},
 'item_price': {7: 11.75},
 'total': {7: 11.75},
 'length': {7: 1172}}

如何获取每行burrito_orders["choice_description"]中的项目数

1 个答案:

答案 0 :(得分:1)

尝试一下:

items = burrito_orders["choice_description"][7]      #Getting string value with items
items = items.replace("[","").replace("]","")        #Taking away the square brackets 
items = items.split(",")                             #Creating a list
no_of_items = len(items)                             #Calculate the length of list 
burrito_orders["length"] = no_of_items               #Add to dataframe