Pandas 笔记

标签: Python 科学计算 发布于:2021-05-21 13:10:00 编辑于:2022-11-15 12:15:40 浏览量:1117

文件的读取与保存

data = pd.read_csv("path/to/csv")
data.to_csv(filename, index=False)

数据预处理

连续值的处理

  1. 归一化:
all_features[numeric_features] = all_features[numeric_features].apply(lambda x: (x - x.mean()) / (x.std()))

对于其中的缺失值:

  1. 将缺失值直接替换为均值:
all_features[numeric_features] = all_features[numeric_features].fillna(0)

离散值的处理

  1. 将离散值转换为指示特征(按可能的选择分解为对应数目的新特征,其值只有 0 或 1):
all_features = pd.get_dummies(all_features, dummy_na=True)

常用操作

将生成的预测值与原 Id 列合并生成一个新的 DataFrame,之后生成提交文件:

submit_df = test_data[['Id']].copy()
submit_df['label'] = pd.Series(preds.reshape(1, -1)[0])
submit_df.to_csv(filename, index=False)

未经允许,禁止转载,本文源站链接:https://iamazing.cn/