1. Import package
pandas as pd
2. Data read, the file is in the code folder
food_info = pd.read_csv ( ' food_info.csv ' )
3. View type
food_info.dtypes
4. View the first five data
food_info.head()
View the first three data
food_info.head ( 3 )
5. View the last four rows of data
food_info.tail ( 4 )
6. View column names
food_info.columns
7. View the dimensions of the matrix
food_info.shape
8. Take out No. 0 data
food_info.loc[0]
Get data using slices
food_info.loc[3:6]
Use index to get data
food_info.loc['Name']
Get multiple columns
columns = ["Price","Name"]
food_info.loc[columns]
9. Put the column names into the list
col_names = food_info.columns.tolist ()
10. Look at the column ending in d
for c in col_names:
c.endswith ( " d " )
11. Discount the product price
food_info["Price"]/10
12. Maximum and minimum mean
food_info["Price"].max()
food_info["Price"].min()
food_info [ " Price " ] .mean ()
13. Sort by a certain column
Ascending order:
food_info.sort_values["Price",inplace=True]
Descending order:
food_info.sort_values [ " Price " , inplace = True, ascending = False]
14 .Check if the value is NaN
price = food_info["Price"]
price_is_null = pd.isnull(price)
food_info[price_is_null]
2020-04-10