以下是食品及其营养成分的数据框架:
import pandas as pd
import os
os.chdir('D:\\userdata\\adbharga\\Desktop\\AVA\\RTestCode\\Python')
data=pd.read_csv("nutrient.csv")
data.head()
出[30]:
Name Calories Fat Carb Fiber Protein
0 Chonga Bagel 300 5 50 3 12
1 8-Grain Roll 380 6 70 7 10
2 Almond Croissant 410 22 45 3 10
3 Apple Fritter 460 23 56 2 7
4 Banana Nut Bread 420 22 52 2 6
需要提取最高的营养成分及其价值。在下面的代码中使用。你知道吗
data['Top Nutrient'] = data[['Calories','Fat','Carb','Fiber','Protein']].idxmax(axis=1)
data['Amount']= data[['Calories','Fat','Carb','Fiber','Protein']].max(axis=1)
data.head()
出[33]:
Name Calories Fat Carb Fiber Protein Top Nutrient Amount
0 Chonga Bagel 300 5 50 3 12 Calories 300
1 8-Grain Roll 380 6 70 7 10 Calories 380
2 Almond Croissant 410 22 45 3 10 Calories 410
3 Apple Fritter 460 23 56 2 7 Calories 460
4 Banana Nut Bread 420 22 52 2 6 Calories 420
有没有办法展示下两个顶级营养素及其作用值。应为输出如下:
Name NextTop2 NextTop2Amount
Chonga Bagel Carb|Protein 50|12
8-Grain Roll Carb|Protein 70|10
Almond Croissant Carb|Fat 45|22
Apple Fritter Carb|Fat 56|23
Banana Nut Bread Carb|Fat 52|22
谢谢
这里是最好的使用^{} 因为非常快。你知道吗
首先按
subset
-[]
筛选列,然后按argsort
获取索引以获取2。和3。顶部:也可以按索引选择值:
最后为一列创建
DataFrame
并添加join by|
:相关问题 更多 >
编程相关推荐