Pandas_2索引与计算

import pandas as pd
food_info = pd.read_csv('./data/food_info.csv')
print(food_info.shape)
#first_row = food_info.loc[0]
first_rows = food_info.loc[3:5]
print(first_rows)
idx = [1,2,4]
first_rows = food_info.loc[idx]
print(first_rows)
(8618, 36)
   NDB_No     Shrt_Desc  Water_(g)  Energ_Kcal  Protein_(g)  Lipid_Tot_(g)  \
3    1004   CHEESE BLUE      42.41         353        21.40          28.74   
4    1005  CHEESE BRICK      41.11         371        23.24          29.68   
5    1006   CHEESE BRIE      48.42         334        20.75          27.68   

   Ash_(g)  Carbohydrt_(g)  Fiber_TD_(g)  Sugar_Tot_(g)       ...        \
3     5.11            2.34           0.0           0.50       ...         
4     3.18            2.79           0.0           0.51       ...         
5     2.70            0.45           0.0           0.45       ...         

   Vit_A_IU  Vit_A_RAE  Vit_E_(mg)  Vit_D_mcg  Vit_D_IU  Vit_K_(mcg)  \
3     721.0      198.0        0.25        0.5      21.0          2.4   
4    1080.0      292.0        0.26        0.5      22.0          2.5   
5     592.0      174.0        0.24        0.5      20.0          2.3   

   FA_Sat_(g)  FA_Mono_(g)  FA_Poly_(g)  Cholestrl_(mg)  
3      18.669        7.778        0.800            75.0  
4      18.764        8.598        0.784            94.0  
5      17.410        8.013        0.826           100.0  

[3 rows x 36 columns]
   NDB_No                 Shrt_Desc  Water_(g)  Energ_Kcal  Protein_(g)  \
1    1002  BUTTER WHIPPED WITH SALT      15.87         717         0.85   
2    1003      BUTTER OIL ANHYDROUS       0.24         876         0.28   
4    1005              CHEESE BRICK      41.11         371        23.24   

   Lipid_Tot_(g)  Ash_(g)  Carbohydrt_(g)  Fiber_TD_(g)  Sugar_Tot_(g)  \
1          81.11     2.11            0.06           0.0           0.06   
2          99.48     0.00            0.00           0.0           0.00   
4          29.68     3.18            2.79           0.0           0.51   

        ...        Vit_A_IU  Vit_A_RAE  Vit_E_(mg)  Vit_D_mcg  Vit_D_IU  \
1       ...          2499.0      684.0        2.32        1.5      60.0   
2       ...          3069.0      840.0        2.80        1.8      73.0   
4       ...          1080.0      292.0        0.26        0.5      22.0   

   Vit_K_(mcg)  FA_Sat_(g)  FA_Mono_(g)  FA_Poly_(g)  Cholestrl_(mg)  
1          7.0      50.489       23.426        3.012           219.0  
2          8.6      61.924       28.732        3.694           256.0  
4          2.5      18.764        8.598        0.784            94.0  

[3 rows x 36 columns]
#print(food_info['NDB_No'])
columns = ['NDB_No','Water_(g)']
print(food_info[columns].head())
   NDB_No  Water_(g)
0    1001      15.87
1    1002      15.87
2    1003       0.24
3    1004      42.41
4    1005      41.11
col_names = food_info.columns.tolist()
print(col_names)
['NDB_No', 'Shrt_Desc', 'Water_(g)', 'Energ_Kcal', 'Protein_(g)', 'Lipid_Tot_(g)', 'Ash_(g)', 'Carbohydrt_(g)', 'Fiber_TD_(g)', 'Sugar_Tot_(g)', 'Calcium_(mg)', 'Iron_(mg)', 'Magnesium_(mg)', 'Phosphorus_(mg)', 'Potassium_(mg)', 'Sodium_(mg)', 'Zinc_(mg)', 'Copper_(mg)', 'Manganese_(mg)', 'Selenium_(mcg)', 'Vit_C_(mg)', 'Thiamin_(mg)', 'Riboflavin_(mg)', 'Niacin_(mg)', 'Vit_B6_(mg)', 'Vit_B12_(mcg)', 'Vit_A_IU', 'Vit_A_RAE', 'Vit_E_(mg)', 'Vit_D_mcg', 'Vit_D_IU', 'Vit_K_(mcg)', 'FA_Sat_(g)', 'FA_Mono_(g)', 'FA_Poly_(g)', 'Cholestrl_(mg)']
gram_cols = []
for c in col_names:
    if c.endswith('(g)'):
        gram_cols.append(c)
gram_res = food_info[gram_cols]
print(gram_res.head())
   Water_(g)  Protein_(g)  Lipid_Tot_(g)  Ash_(g)  Carbohydrt_(g)  \
0      15.87         0.85          81.11     2.11            0.06   
1      15.87         0.85          81.11     2.11            0.06   
2       0.24         0.28          99.48     0.00            0.00   
3      42.41        21.40          28.74     5.11            2.34   
4      41.11        23.24          29.68     3.18            2.79   

   Fiber_TD_(g)  Sugar_Tot_(g)  FA_Sat_(g)  FA_Mono_(g)  FA_Poly_(g)  
0           0.0           0.06      51.368       21.021        3.043  
1           0.0           0.06      50.489       23.426        3.012  
2           0.0           0.00      61.924       28.732        3.694  
3           0.0           0.50      18.669        7.778        0.800  
4           0.0           0.51      18.764        8.598        0.784  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值