import pandas food_info = pandas.read_csv("E:\Python\ku\pandas/food_info.csv") #打开一个csv数据文件 print(type(food_info)) print(food_info.dtypes) first_rows = food_info.head(3) #括号中表示前几行 print(first_rows) last_rows = food_info.tail(4) #括号中表示后几行 print(last_rows) print(food_info.columns) #返回所有的列名 print(food_info.shape) #返回总行数,总列数 #取数据 #行 print(food_info.loc[0]) #返回不包括列名的第一行的值 print(food_info.loc[3:6]) #返回3,4,5,6行 #列(默认第一行是列名) print(food_info["NDB_No"]) #返回"NDB_No"列的所有数据 #取多列 columns = ["Zinc_(mg)", "Copper_(mg)"] zinc_copper = food_info[columns] print(zinc_copper) #列出有“(g)”关键字的列 columns = food_info.columns.tolist() #转list print(columns) gram_columns = [] for c in columns: if c.endswith("(g)"): gram_columns.append(c) #带“(g)”的列名写到gram_columns中 gram_df = food_info[gram_columns] print(gram_df.head(3)) #列的计算 print(food_info["Iron_(mg)"]) div_1000 = food_info["Iron_(mg)"] / 1000 print(div_1000)
E:\Python\venv\Scripts\python.exe E:/Python/ku/pandas/1.基础.py
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
NDB_No Shrt_Desc Water_(g) Energ_Kcal Protein_(g) \
0 1001 BUTTER WITH SALT 15.87 717 0.85
1 1002 BUTTER WHIPPED WITH SALT 15.87 717 0.85
2 1003 BUTTER OIL ANHYDROUS 0.24 876 0.28
Lipid_Tot_(g) Ash_(g) Carbohydrt_(g) Fiber_TD_(g) Sugar_Tot_(g) \
0 81.11 2.11 0.06 0.0 0.06
1 81.11 2.11 0.06 0.0 0.06
2 99.48 0.00 0.00 0.0 0.00
... Vit_A_IU Vit_A_RAE Vit_E_(mg) Vit_D_mcg Vit_D_IU \
0 ... 2499.0 684.0 2.32 1.5 60.0
1 ... 2499.0 684.0 2.32 1.5 60.0
2 ... 3069.0 840.0 2.80 1.8 73.0
Vit_K_(mcg) FA_Sat_(g) FA_Mono_(g) FA_Poly_(g) Cholestrl_(mg)
0 7.0 51.368 21.021 3.043 215.0
1 7.0 50.489 23.426 3.012 219.0
2 8.6 61.924 28.732 3.694 256.0
[3 rows x 36 columns]
NDB_No Shrt_Desc Water_(g) Energ_Kcal Protein_(g) \
8614 90240 SCALLOP (BAY&SEA) CKD STMD 70.25 111 20.54
8615 90480 SYRUP CANE 26.00 269 0.00
8616 90560 SNAIL RAW 79.20 90 16.10
8617 93600 TURTLE GREEN RAW 78.50 89 19.80
Lipid_Tot_(g) Ash_(g) Carbohydrt_(g) Fiber_TD_(g) Sugar_Tot_(g) \
8614 0.84 2.97 5.41 0.0 0.0
8615 0.00 0.86 73.14 0.0 73.2
8616 1.40 1.30 2.00 0.0 0.0
8617 0.50 1.20 0.00 0.0 0.0
... Vit_A_IU Vit_A_RAE Vit_E_(mg) Vit_D_mcg Vit_D_IU \
8614 ... 5.0 2.0 0.0 0.0 2.0
8615 ... 0.0 0.0 0.0 0.0 0.0
8616 ... 100.0 30.0 5.0 0.0 0.0
8617 ... 100.0 30.0 0.5 0.0 0.0
Vit_K_(mcg) FA_Sat_(g) FA_Mono_(g) FA_Poly_(g) Cholestrl_(mg)
8614 0.0 0.218 0.082 0.222 41.0
8615 0.0 0.000 0.000 0.000 0.0
8616 0.1 0.361 0.259 0.252 50.0
8617 0.1 0.127 0.088 0.170 50.0
[4 rows x 36 columns]
Index(['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)'],
dtype='object')
(8618, 36)
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
NDB_No Shrt_Desc Water_(g) Energ_Kcal Protein_(g) \
3 1004 CHEESE BLUE 42.41 353 21.40
4 1005 CHEESE BRICK 41.11 371 23.24
5 1006 CHEESE BRIE 48.42 334 20.75
6 1007 CHEESE CAMEMBERT 51.80 300 19.80
Lipid_Tot_(g) Ash_(g) Carbohydrt_(g) Fiber_TD_(g) Sugar_Tot_(g) \
3 28.74 5.11 2.34 0.0 0.50
4 29.68 3.18 2.79 0.0 0.51
5 27.68 2.70 0.45 0.0 0.45
6 24.26 3.68 0.46 0.0 0.46
... Vit_A_IU Vit_A_RAE Vit_E_(mg) Vit_D_mcg Vit_D_IU \
3 ... 721.0 198.0 0.25 0.5 21.0
4 ... 1080.0 292.0 0.26 0.5 22.0
5 ... 592.0 174.0 0.24 0.5 20.0
6 ... 820.0 241.0 0.21 0.4 18.0
Vit_K_(mcg) FA_Sat_(g) FA_Mono_(g) FA_Poly_(g) Cholestrl_(mg)
3 2.4 18.669 7.778 0.800 75.0
4 2.5 18.764 8.598 0.784 94.0
5 2.3 17.410 8.013 0.826 100.0
6 2.0 15.259 7.023 0.724 72.0
[4 rows x 36 columns]
0 1001
1 1002
2 1003
3 1004
4 1005
5 1006
6 1007
7 1008
8 1009
9 1010
10 1011
11 1012
12 1013
13 1014
14 1015
15 1016
16 1017
17 1018
18 1019
19 1020
20 1021
21 1022
22 1023
23 1024
24 1025
25 1026
26 1027
27 1028
28 1029
29 1030
...
8588 43544
8589 43546
8590 43550
8591 43566
8592 43570
8593 43572
8594 43585
8595 43589
8596 43595
8597 43597
8598 43598
8599 44005
8600 44018
8601 44048
8602 44055
8603 44061
8604 44074
8605 44110
8606 44158
8607 44203
8608 44258
8609 44259
8610 44260
8611 48052
8612 80200
8613 83110
8614 90240
8615 90480
8616 90560
8617 93600
Name: NDB_No, Length: 8618, dtype: int64
Zinc_(mg) Copper_(mg)
0 0.09 0.000
1 0.05 0.016
2 0.01 0.001
3 2.66 0.040
4 2.60 0.024
5 2.38 0.019
6 2.38 0.021
7 2.94 0.024
8 3.43 0.056
9 2.79 0.042
10 3.07 0.042
11 0.40 0.029
12 0.33 0.040
13 0.47 0.030
14 0.51 0.033
15 0.38 0.028
16 0.51 0.019
17 3.75 0.036
18 2.88 0.032
19 3.50 0.025
20 1.14 0.080
21 3.90 0.036
22 3.90 0.032
23 2.10 0.021
24 3.00 0.032
25 2.92 0.011
26 2.46 0.022
27 2.76 0.025
28 3.61 0.034
29 2.81 0.031
... ... ...
8588 3.30 0.377
8589 0.05 0.040
8590 0.05 0.030
8591 1.15 0.116
8592 5.03 0.200
8593 3.83 0.545
8594 0.08 0.035
8595 3.90 0.027
8596 4.10 0.100
8597 3.13 0.027
8598 0.13 0.000
8599 0.02 0.000
8600 0.09 0.037
8601 0.21 0.026
8602 2.77 0.571
8603 0.41 0.838
8604 0.05 0.028
8605 0.03 0.023
8606 0.10 0.112
8607 0.02 0.020
8608 1.49 0.854
8609 0.19 0.040
8610 0.10 0.038
8611 0.85 0.182
8612 1.00 0.250
8613 1.10 0.100
8614 1.55 0.033
8615 0.19 0.020
8616 1.00 0.400
8617 1.00 0.250
[8618 rows x 2 columns]
['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)']
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
0 0.02
1 0.16
2 0.00
3 0.31
4 0.43
5 0.50
6 0.33
7 0.64
8 0.16
9 0.21
10 0.76
11 0.07
12 0.16
13 0.15
14 0.13
15 0.14
16 0.38
17 0.44
18 0.65
19 0.23
20 0.52
21 0.24
22 0.17
23 0.13
24 0.72
25 0.44
26 0.20
27 0.22
28 0.23
29 0.41
...
8588 9.00
8589 0.30
8590 0.10
8591 1.63
8592 34.82
8593 2.28
8594 0.17
8595 0.17
8596 4.86
8597 0.25
8598 0.23
8599 0.13
8600 0.11
8601 0.68
8602 7.83
8603 3.11
8604 0.30
8605 0.18
8606 0.80
8607 0.04
8608 3.87
8609 0.05
8610 0.38
8611 5.20
8612 1.50
8613 1.40
8614 0.58
8615 3.60
8616 3.50
8617 1.40
Name: Iron_(mg), Length: 8618, dtype: float64
0 0.00002
1 0.00016
2 0.00000
3 0.00031
4 0.00043
5 0.00050
6 0.00033
7 0.00064
8 0.00016
9 0.00021
10 0.00076
11 0.00007
12 0.00016
13 0.00015
14 0.00013
15 0.00014
16 0.00038
17 0.00044
18 0.00065
19 0.00023
20 0.00052
21 0.00024
22 0.00017
23 0.00013
24 0.00072
25 0.00044
26 0.00020
27 0.00022
28 0.00023
29 0.00041
...
8588 0.00900
8589 0.00030
8590 0.00010
8591 0.00163
8592 0.03482
8593 0.00228
8594 0.00017
8595 0.00017
8596 0.00486
8597 0.00025
8598 0.00023
8599 0.00013
8600 0.00011
8601 0.00068
8602 0.00783
8603 0.00311
8604 0.00030
8605 0.00018
8606 0.00080
8607 0.00004
8608 0.00387
8609 0.00005
8610 0.00038
8611 0.00520
8612 0.00150
8613 0.00140
8614 0.00058
8615 0.00360
8616 0.00350
8617 0.00140
Name: Iron_(mg), Length: 8618, dtype: float64
Process finished with exit code 0