【发布时间】:2020-07-28 18:58:49
【问题描述】:
我在 pandas 中有以下数据框:
test = pd.DataFrame({'Food': ['Apple Cake Apple', 'Orange Tomato Cake', 'Broccoli Apple Orange', 'Cake Orange Cake', 'Tomato Apple Orange'], 'Type' : ['Fruit Dessert', 'Fruit Veggie', 'Veggie Fruit', 'Dessert Fruit', 'Veggie Fruit']})
test
Food Type
0 Apple Cake Apple Fruit Dessert Fruit
1 Orange Tomato Fruit Veggie Dessert
2 Broccoli Apple Orange Veggie Fruit Fruit
3 Cake Orange Cake Dessert Fruit Dessert
4 Tomato Apple Orange Veggie Fruit Fruit
我想创建一个新列来计算“类型”列中的值,并将它们从大到小排序,无论食物类型如何。例如,这正是我正在寻找的:
test = pd.DataFrame({'Food': ['Apple Cake Apple', 'Orange Tomato Cake', 'Broccoli Apple Orange', 'Cake Orange Cake', 'Tomato Apple Orange'],
'Type' : ['Fruit Dessert Fruit', 'Fruit Veggie Dessert', 'Veggie Fruit Fruit', 'Dessert Fruit Dessert', 'Veggie Fruit Fruit'],
'Count': ['2 1', '1 1 1 ', '2 1', '2 1', '2 1']})
test
Food Type Count
0 Apple Cake Apple Fruit Dessert Fruit 2 1
1 Orange Tomato Cake Fruit Veggie Dessert 1 1 1
2 Broccoli Apple Orange Veggie Fruit Fruit 2 1
3 Cake Orange Cake Dessert Fruit Dessert 2 1
4 Tomato Apple Orange Veggie Fruit Fruit 2 1
我该怎么做呢?非常感谢!
【问题讨论】:
标签: python regex string pandas string-formatting