<jupyter_start><jupyter_text>Chapter 3. Pandas 数据处理<jupyter_code>import pandas as pd
import numpy as np

food_info = pd.read_csv("food_info.csv")
print(type(food_info))  # DataFrame

# Data types: 
# object - For string values 
# int - For integer values 
# float - For float values 
# datetime - For time values 
# bool - For Boolean values 
print(food_info.dtypes)

# DataFrame.head([num])  default_num = 5
# DataFrame.tail([num])  default_num = 5

head = food_info.head(5)
print(head)

print('-----------------------')
print(food_info.columns)  #columns name
print(type(food_info.columns))
print('-----------------------')
print(food_info.shape)

# get row values
print(food_info.loc[0])
# print('-----------------------')
# print(food_info.loc[2])
# print('-----------------------')
# print(food_info.loc[5])

# get serveral row values
row = [1, 2, 5]
print(food_info.loc[row])

# get column values
col_name = 'Vit_A_IU'
Vit_A_IU = food_info[col_name]
Vit_A_IU[0:10]

# get several column values

columns = ['Iron_(mg)', 'Magnesium_(mg)']
values = food_info[columns]
print(values[0:10])

# cut part of the DataFrame
food_info.loc[2:5]

# Filt some of the columns
# in this case- end up with 'g'
col_names = food_info.columns.tolist()  #get columns in the list
print(col_names)
print('\n\n')

gram_columns = []  #get conditional index

for col in col_names:
    if col.endswith('(g)'):
        gram_columns.append(col)

gram_data = food_info[gram_columns]
print(gram_data[0:10])

# basic operations
new = pd.DataFrame()
new['Sodium_(mg)'] = food_info['Sodium_(mg)']
new['add_100'] = food_info['Sodium_(mg)'] + 100
new['sub_100'] = food_info['Sodium_(mg)'] - 100
new['multiple_2'] = food_info['Sodium_(mg)'] * 2
new['div_1000'] = food_info['Sodium_(mg)'] / 1000
print(new[0:10])

# operations between DataFrames
new = pd.DataFrame()
new['water_energy'] = food_info['Water_(g)']
new['Energ_Kcal'] = food_info['Energ_Kcal']
new['total'] = new['water_energy'] * new['Energ_Kcal']
print(new[0:10])

# max(), min(), mean()
# normalization
max_calories = food_info["Energ_Kcal"].max()
# min_calories = food_info["Energ_Kcal"].min()
# Divide the values in "Energ_Kcal" by the largest value.
normalized_calories = food_info["Energ_Kcal"] / max_calories
normalized_protein = food_info["Protein_(g)"] / food_info["Protein_(g)"].max()
normalized_fat = food_info["Lipid_Tot_(g)"] / food_info["Lipid_Tot_(g)"].max()
food_info["Normalized_Protein"] = normalized_protein
food_info["Normalized_Fat"] = normalized_fat

# sort()
#By default, pandas will sort the data by the column we specify in ascending order and return a new DataFrame
# Sorts the DataFrame in-place, rather than returning a new DataFrame.
# print( food_info["Sodium_(mg)"])
food_info.sort_values("Sodium_(mg)", inplace=True)
print(food_info["Sodium_(mg)"][0:10])  # ascending， default = ture
print('------------------------')
a = food_info.sort_values("Sodium_(mg)", ascending=False)  #descending， default = ture
print(a["Sodium_(mg)"][0:10])<jupyter_output>760     0.0
758     0.0
405     0.0
761     0.0
2269    0.0
763     0.0
764     0.0
770     0.0
774     0.0
396     0.0
Name: Sodium_(mg), dtype: float64
------------------------
276     38758.0
5814    27360.0
6192    26050.0
1242    26000.0
1245    24000.0
1243    24000.0
1244    23875.0
292     17000.0
1254    11588.0
5811    10600.0
Name: Sodium_(mg), dtype: float64
{'fraction_non_alphanumeric': 0.18581979320531758, 'fraction_numerical': 0.06144756277695716, 'mean_word_length': 3.964809384164223, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '69411', 'license': 'Apache-2.0', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1247, 'n_words': 286, 'path': 'Yudi TANG/Packages/Pandas/Pandas.ipynb', 'repo_name': 'JKChang2015/Machine_Learning', 'size': '3385'}