Appearance
健康与营养咨询数据预处理与数据规范设计
python
import pandas as pd
# 加载数据集
data = __________
# 查看表结构基本信息
print(__________)
# 显示每一列的空缺值数量
print(__________)
# 删除含有缺失值的行
data_cleaned = __________
# 转换 'Your age' 列的数据类型为整数类型,并处理异常值
data_cleaned.loc[:, 'Your age'] = __________(__________, errors='coerce')
data_cleaned = data_cleaned.dropna(subset=['Your age'])
data_cleaned = data_cleaned[data_cleaned['Your age'] >= 0]
data_cleaned.loc[:, 'Your age'] = data_cleaned['Your age'].__________
print(data_cleaned['Your age'].dtype)
# 检查和删除重复值
duplicates_removed = data_cleaned.duplicated().sum()
data_cleaned = __________
print(f"Removed {duplicates_removed} duplicate rows")
from sklearn.preprocessing import LabelEncoder
# 归一化 'How do you describe your current level of fitness ?' 列
label_encoder = LabelEncoder()
data_cleaned[__________] = __________
print(data_cleaned['How do you describe your current level of fitness ?'].unique())
from sklearn.preprocessing import LabelEncoder
import matplotlib.pyplot as plt
# 去掉列名中的空格
data.columns = data.columns.str.strip()
# 显示数据集的列名
print(data.columns)
# 删除包含缺失值的行
data_cleaned = data.dropna(subset=['How often do you exercise?'])
# 统计不同健身频率的分布情况
exercise_frequency_counts = data_cleaned['How often do you exercise?'].value_counts()
# 绘制饼图
plt.figure(figsize=(10, 6))
__________(autopct='%1.1f%%', startangle=90, colors=plt.cm.Paired.colors)
plt.title('Distribution of Exercise Frequency')
plt.ylabel('')
plt.show()
import pandas as pd
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
# 填充缺失值
data_filled = data.apply(lambda x: x.fillna(x.mode()[0]))
# 划分数据(测试集占比20%)
train_data, test_data = __________(__________, random_state=42)
# 保存处理后的数据
cleaned_file_path = '__________'
__________(__________, index=False)