Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从Dataframe中的列表提取元组_Python_List_Tuples_Iteration - Fatal编程技术网

Python 从Dataframe中的列表提取元组

Python 从Dataframe中的列表提取元组,python,list,tuples,iteration,Python,List,Tuples,Iteration,我有一个12列的数据框。我想根据另一列的值提取一列的行 我的数据帧示例 order_id order_type order_items 45 Lunch [('Burger', 5), ('Fries', 6)] 12 Dinner [('Shrimp', 10), ('Fish&Chips', 7)] 44 Lunch [('Salad', 9), ('Steak', 9)] 23

我有一个12列的数据框。我想根据另一列的值提取一列的行

我的数据帧示例

order_id    order_type   order_items
45           Lunch       [('Burger', 5), ('Fries', 6)]
12           Dinner      [('Shrimp', 10), ('Fish&Chips', 7)]
44           Lunch       [('Salad', 9), ('Steak', 9)]
23           Breakfast   [('Coffee', 2), ('Eggs', 3)]
我想通过提取每个
元组的第一项来提取早餐、午餐和晚餐菜单。
并从元组中的下一项中提取订单数

根据这行代码,每个项目都是字符串类型

print(type(df['order_items'][0]))
>> <class 'str'>
但是输出是这样的,我不能使用
for循环
迭代子列表并访问元组

2                           [('Coffee', 4), ('Eggs', 7)]
7                           [('Coffee', 2), ('Eggs', 3)]
8      [('Cereal', 7), ('Pancake', 8), ('Coffee', 4),...
9      [('Cereal', 3), ('Eggs', 1), ('Coffee', 1), ('...
我还尝试转换为
列表

orderTypeLst = df(['order_type'])['order_items'].apply(list)
然后通过执行以下操作提取列表:

breakFast=orderTypeLst['Breakfast']
lunch=orderTypeLst['Lunch']
dinner=orderTypeLst['Dinner']
但是输出是一个字符串。我也不能重复这一点

["[('Coffee', 4), ('Eggs', 7)]",
 "[('Coffee', 2), ('Eggs', 3)]",
 "[('Cereal', 7), ('Pancake', 8), ('Coffee', 4), ('Eggs', 8)]"]
至于
字典
我尝试了以下方法,但输出重复:

pd.Series(outlierFile.order_type.values,index=outlierFile.order_items).to_dict()
输出样本

 "[('Fries', 1), ('Steak', 6), ('Salad', 8), ('Chicken', 10)]": 'Lunch',
 "[('Cereal', 6), ('Pancake', 8), ('Eggs', 3)]": 'Breakfast',
 "[('Shrimp', 9), ('Salmon', 9)]": 'Dinner',
 "[('Pancake', 3), ('Coffee', 5)]": 'Breakfast',
 "[('Eggs', 1), ('Pancake', 1), ('Coffee', 5), ('Cereal', 5)]": 'Breakfast'
我想要的输出是每个order_类型(列表或字典)的干净版本,因此我可以迭代元组并提取所需的项

任何意见都会有帮助
谢谢,

IIUC,请在评估后尝试使用
pandas.DataFrame.groupby

my_dict = df.groupby('order_type')['order_items'].apply(lambda x: sum(x, [])).to_dict()
print(my_dict)
输出:

{'Breakfast': [('Coffee', 2), ('Eggs', 3)],
 'Dinner': [('Shrimp', 10), ('Fish&Chips', 7)],
 'Lunch': [('Burger', 5), ('Fries', 6), ('Salad', 9), ('Steak', 9)]}

df['order\u items']
字符串吗?每个项都是元组列表。样本输出:
[('Shrimp',10),('Fish&Chips',7)]
。类型是string。根据您的编辑,我想它确实是str?是的,我刚刚检查过谢谢克里斯。但是这会返回
ValueError:格式错误的节点或字符串:[('Shrimp',10),('Fish&Chips',7)]
是否来自应用行?我认为这是由第一行中的第一个应用程序引起的。似乎有一个无效的python对象,而您的注释中的元素看起来很好。我有大约500行,它们看起来都很像我。您认为无效对象可能是什么?
{'Breakfast': [('Coffee', 2), ('Eggs', 3)],
 'Dinner': [('Shrimp', 10), ('Fish&Chips', 7)],
 'Lunch': [('Burger', 5), ('Fries', 6), ('Salad', 9), ('Steak', 9)]}