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分析apache访问日志脚本分享

#!/usr/bin/env python # coding=utf-8 #---------------------------------------------------...

Pandas统计重复的列里面的值方法

pandas 代码如下: import pandas as pd import numpy as np salaries = pd.DataFrame({ 'name': ['B...

Django错误:TypeError at / 'bool' object is not callable解决

使用 Django自带的 auth 用户验证功能,编写函数,使用 is_authenticated 检查用户是否登录,结果报错: TypeError at / 'bool' object...

操作Windows注册表的简单的Python程序制作教程

通过Python操作注册表有两种方式,第一种是通过Python的内置模块 _winreg;另一种方式就是Win32 Extension For Python 的win32api模块,但是...

python定时检查某个进程是否已经关闭的方法

本文实例讲述了python定时检查某个进程是否已经关闭的方法。分享给大家供大家参考。具体如下: import threading import time import os impo...