pandas数据集的端到端处理

yipeiwu_com6年前Python基础

1. 数据集基本信息

df = pd.read_csv()

df.head():前五行;

df.info():

  • rangeindex:行索引;
  • data columns:列索引;
  • dtypes:各个列的类型,
  • 主体部分是各个列值的情况,比如可判断是否存在 NaN 值;

对于非数值型的属性列

  • df[‘some_categorical_columns'].value_counts():取值分布;

df.describe(): 各个列的基本统计信息

  • count
  • mean
  • std
  • min/max
  • 25%, 50%, 75%:分位数

df.hist(bins=50, figsize=(20, 15)):统计直方图;

对 df 的每一列进行展示:

train_prices = pd.DataFrame({'price': train_df.SalePrice, 
    'log(price+1)': np.log1p(train_df.SalePrice)})
 # train_prices 共两列,一列列名为 price,一列列名为 log(price+1)
train_prices.hist()

2. 数据集拆分

def split_train_test(data, test_ratio=.3):
 shuffled_indices = np.random.permutation(len(data))
 test_size = int(len(data)*test_ratio)
 test_indices = shuffled_indices[:test_size]
 train_indices = shuffled_indices[test_size:]
 return data.iloc[train_indices], data.iloc[test_indices]

3. 数据预处理

  • 一键把 categorical 型特征(字符串类型)转化为数值型:
>> df['label'] = pd.Categorical(df['label']).codes
  • 一键把 categorical 型特征(字符串类型)转化为 one-hot 编码:
>> df = pd.get_dummies(df)
  • null 值统计与填充:
>> df.isnull().sum().sort_values(ascending=False).head()
# 填充为 mean 值
>> mean_cols = df.mean()
>> df = df.fillna(mean_cols)
>> df.isnull().sum().sum()
0

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对【听图阁-专注于Python设计】的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

Python中的ConfigParser模块使用详解

1.基本的读取配置文件     -read(filename) 直接读取ini文件内容     -sections() 得到所...

python Jupyter运行时间实例过程解析

这篇文章主要介绍了python Jupyter运行时间实例过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.Python t...

python用字典统计单词或汉字词个数示例

有如下格式的文本文件复制代码 代码如下:/“/请/!/”/“/请/!/”/两名/剑士/各自/倒转/剑尖/,/右手/握/剑柄/,/左手/搭于/右手/手背/,/躬身行礼/。/两/人/身子/尚...

Pytorch 实现focal_loss 多类别和二分类示例

我就废话不多说了,直接上代码吧! import numpy as np import torch import torch.nn as nn import torch.nn.func...

python实现SOM算法

python实现SOM算法

算法简介 SOM网络是一种竞争学习型的无监督神经网络,将高维空间中相似的样本点映射到网络输出层中的邻近神经元。 训练过程简述:在接收到训练样本后,每个输出层神经元会计算该样本与自身携带的...