import pandas as pd
food_info = pd.read_csv("D:\\test\\food_info.csv") #此处需要转义
print (type(food_info))
first_row = food_info.head() #默认是前5行
print (food_info.shape)
(8618, 36)
print (food_info.loc[0]) #读取第一行
NDB_No 1001 Shrt_Desc BUTTER WITH SALT Water_(g) 15.87 Energ_Kcal 717 Protein_(g) 0.85 Lipid_Tot_(g) 81.11 Ash_(g) 2.11 Carbohydrt_(g) 0.06 Fiber_TD_(g) 0 Sugar_Tot_(g) 0.06 Calcium_(mg) 24 Iron_(mg) 0.02 Magnesium_(mg) 2 Phosphorus_(mg) 24 Potassium_(mg) 24 Sodium_(mg) 643 Zinc_(mg) 0.09 Copper_(mg) 0 Manganese_(mg) 0 Selenium_(mcg) 1 Vit_C_(mg) 0 Thiamin_(mg) 0.005 Riboflavin_(mg) 0.034 Niacin_(mg) 0.042 Vit_B6_(mg) 0.003 Vit_B12_(mcg) 0.17 Vit_A_IU 2499 Vit_A_RAE 684 Vit_E_(mg) 2.32 Vit_D_mcg 1.5 Vit_D_IU 60 Vit_K_(mcg) 7 FA_Sat_(g) 51.368 FA_Mono_(g) 21.021 FA_Poly_(g) 3.043 Cholestrl_(mg) 215 Name: 0, dtype: object
food_info.loc[1:3]#不用print就会以表格显示,并且与python不一样的的是第三行也显示了
NDB_No | Shrt_Desc | Water_(g) | Energ_Kcal | Protein_(g) | Lipid_Tot_(g) | Ash_(g) | Carbohydrt_(g) | Fiber_TD_(g) | Sugar_Tot_(g) | … | 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) | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | 1002 | BUTTER WHIPPED WITH SALT | 15.87 | 717 | 0.85 | 81.11 | 2.11 | 0.06 | 0.0 | 0.06 | … | 2499.0 | 684.0 | 2.32 | 1.5 | 60.0 | 7.0 | 50.489 | 23.426 | 3.012 | 219.0 |
2 | 1003 | BUTTER OIL ANHYDROUS | 0.24 | 876 | 0.28 | 99.48 | 0.00 | 0.00 | 0.0 | 0.00 | … | 3069.0 | 840.0 | 2.80 | 1.8 | 73.0 | 8.6 | 61.924 | 28.732 | 3.694 | 256.0 |
3 | 1004 | CHEESE BLUE | 42.41 | 353 | 21.40 | 28.74 | 5.11 | 2.34 | 0.0 | 0.50 | … | 721.0 | 198.0 | 0.25 | 0.5 | 21.0 | 2.4 | 18.669 | 7.778 | 0.800 | 75.0 |
3 rows × 36 columns
label = [2,5,10]
food_info.loc[label] #只显示2,5,10行
NDB_No | Shrt_Desc | Water_(g) | Energ_Kcal | Protein_(g) | Lipid_Tot_(g) | Ash_(g) | Carbohydrt_(g) | Fiber_TD_(g) | Sugar_Tot_(g) | … | 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) | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2 | 1003 | BUTTER OIL ANHYDROUS | 0.24 | 876 | 0.28 | 99.48 | 0.00 | 0.00 | 0.0 | 0.00 | … | 3069.0 | 840.0 | 2.80 | 1.8 | 73.0 | 8.6 | 61.924 | 28.732 | 3.694 | 256.0 |
5 | 1006 | CHEESE BRIE | 48.42 | 334 | 20.75 | 27.68 | 2.70 | 0.45 | 0.0 | 0.45 | … | 592.0 | 174.0 | 0.24 | 0.5 | 20.0 | 2.3 | 17.410 | 8.013 | 0.826 | 100.0 |
10 | 1011 | CHEESE COLBY | 38.20 | 394 | 23.76 | 32.11 | 3.36 | 2.57 | 0.0 | 0.52 | … | 994.0 | 264.0 | 0.28 | 0.6 | 24.0 | 2.7 | 20.218 | 9.280 | 0.953 | 95.0 |
3 rows × 36 columns
colums = ['Shrt_Desc','NDB_No']
food_info[colums] #指定找某些列
Shrt_Desc | NDB_No | |
---|---|---|
0 | BUTTER WITH SALT | 1001 |
1 | BUTTER WHIPPED WITH SALT | 1002 |
2 | BUTTER OIL ANHYDROUS | 1003 |
3 | CHEESE BLUE | 1004 |
4 | CHEESE BRICK | 1005 |
5 | CHEESE BRIE | 1006 |
6 | CHEESE CAMEMBERT | 1007 |
7 | CHEESE CARAWAY | 1008 |
8 | CHEESE CHEDDAR | 1009 |
9 | CHEESE CHESHIRE | 1010 |
10 | CHEESE COLBY | 1011 |
11 | CHEESE COTTAGE CRMD LRG OR SML CURD | 1012 |
12 | CHEESE COTTAGE CRMD W/FRUIT | 1013 |
13 | CHEESE COTTAGE NONFAT UNCRMD DRY LRG OR SML CURD | 1014 |
14 | CHEESE COTTAGE LOWFAT 2% MILKFAT | 1015 |
15 | CHEESE COTTAGE LOWFAT 1% MILKFAT | 1016 |
16 | CHEESE CREAM | 1017 |
17 | CHEESE EDAM | 1018 |
18 | CHEESE FETA | 1019 |
19 | CHEESE FONTINA | 1020 |
20 | CHEESE GJETOST | 1021 |
21 | CHEESE GOUDA | 1022 |
22 | CHEESE GRUYERE | 1023 |
23 | CHEESE LIMBURGER | 1024 |
24 | CHEESE MONTEREY | 1025 |
25 | CHEESE MOZZARELLA WHL MILK | 1026 |
26 | CHEESE MOZZARELLA WHL MILK LO MOIST | 1027 |
27 | CHEESE MOZZARELLA PART SKIM MILK | 1028 |
28 | CHEESE MOZZARELLA LO MOIST PART-SKIM | 1029 |
29 | CHEESE MUENSTER | 1030 |
… | … | … |
8588 | BABYFOOD CRL RICE W/ PEARS & APPL DRY INST | 43544 |
8589 | BABYFOOD BANANA NO TAPIOCA STR | 43546 |
8590 | BABYFOOD BANANA APPL DSSRT STR | 43550 |
8591 | SNACKS TORTILLA CHIPS LT (BAKED W/ LESS OIL) | 43566 |
8592 | CEREALS RTE POST HONEY BUNCHES OF OATS HONEY RSTD | 43570 |
8593 | POPCORN MICROWAVE LOFAT&NA | 43572 |
8594 | BABYFOOD FRUIT SUPREME DSSRT | 43585 |
8595 | CHEESE SWISS LOW FAT | 43589 |
8596 | BREAKFAST BAR CORN FLAKE CRUST W/FRUIT | 43595 |
8597 | CHEESE MOZZARELLA LO NA | 43597 |
8598 | MAYONNAISE DRSNG NO CHOL | 43598 |
8599 | OIL CORN PEANUT AND OLIVE | 44005 |
8600 | SWEETENERS TABLETOP FRUCTOSE LIQ | 44018 |
8601 | CHEESE FOOD IMITATION | 44048 |
8602 | CELERY FLAKES DRIED | 44055 |
8603 | PUDDINGS CHOC FLAVOR LO CAL INST DRY MIX | 44061 |
8604 | BABYFOOD GRAPE JUC NO SUGAR CND | 44074 |
8605 | JELLIES RED SUGAR HOME PRESERVED | 44110 |
8606 | PIE FILLINGS BLUEBERRY CND | 44158 |
8607 | COCKTAIL MIX NON-ALCOHOLIC CONCD FRZ | 44203 |
8608 | PUDDINGS CHOC FLAVOR LO CAL REG DRY MIX | 44258 |
8609 | PUDDINGS ALL FLAVORS XCPT CHOC LO CAL REG DRY MIX | 44259 |
8610 | PUDDINGS ALL FLAVORS XCPT CHOC LO CAL INST DRY… | 44260 |
8611 | VITAL WHEAT GLUTEN | 48052 |
8612 | FROG LEGS RAW | 80200 |
8613 | MACKEREL SALTED | 83110 |
8614 | SCALLOP (BAY&SEA) CKD STMD | 90240 |
8615 | SYRUP CANE | 90480 |
8616 | SNAIL RAW | 90560 |
8617 | TURTLE GREEN RAW | 93600 |
8618 rows × 2 columns
print (food_info.dtypes) #显示每一列的数据类型
NDB_No int64
Shrt_Desc object
Water_(g) float64
Energ_Kcal int64
Protein_(g) float64
Lipid_Tot_(g) float64
Ash_(g) float64
Carbohydrt_(g) float64
Fiber_TD_(g) float64
Sugar_Tot_(g) float64
Calcium_(mg) float64
Iron_(mg) float64
Magnesium_(mg) float64
Phosphorus_(mg) float64
Potassium_(mg) float64
Sodium_(mg) float64
Zinc_(mg) float64
Copper_(mg) float64
Manganese_(mg) float64
Selenium_(mcg) float64
Vit_C_(mg) float64
Thiamin_(mg) float64
Riboflavin_(mg) float64
Niacin_(mg) float64
Vit_B6_(mg) float64
Vit_B12_(mcg) float64
Vit_A_IU float64
Vit_A_RAE float64
Vit_E_(mg) float64
Vit_D_mcg float64
Vit_D_IU float64
Vit_K_(mcg) float64
FA_Sat_(g) float64
FA_Mono_(g) float64
FA_Poly_(g) float64
Cholestrl_(mg) float64
dtype: object
col_names = food_info.columns.tolist() #转化成列表
gram_names = []
for c in col_names:
if c.endswith(('(g)')): #以(g)结尾的挑选处理
gram_names.append(c)
gram_df = food_info[gram_names]
print (gram_df.head(3))
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
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