pandas数据集的端到端处理

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接

(0)

相关推荐

  • python:pandas合并csv文件的方法(图书数据集成)

    数据集成:将不同表的数据通过主键进行连接起来,方便对数据进行整体的分析. 两张表:ReaderInformation.csv,ReaderRentRecode.csv ReaderInformation.csv: ReaderRentRecode.csv: pandas读取csv文件,并进行csv文件合并处理: # -*- coding:utf-8 -*- import csv as csv import numpy as np # ------------- # csv读取表格数据 # ---

  • Python字符串逆序的实现方法【一题多解】

    https://www.jb51.net/article/156406.htm https://www.jb51.net/article/156407.htm 1. 使用索引 >> strA = 'abcdefg' >> strA[::-1] 'gfedcba' 2. 使用 list 的 reverse 方法 >> l = [c for c in strA] >> l.reverse() >> ''.join(l) 'gfedcba' 3. 使用

  • Python实现去除列表中重复元素的方法总结【7种方法】

    这里首先给出来我很早之前写的一篇博客,Python实现去除列表中重复元素的方法小结[4种方法],感兴趣的话可以去看看,今天是在实践过程中又积累了一些方法,这里一并总结放在这里. 由于内容很简单,就不再过多说明了,这里直接上代码,具体如下: # !/usr/bin/env python # -*- coding:utf-8 -*- ''' __Author__:沂水寒城 功能: python列表去除方法总结(7种方法) ''' import sys reload(sys) import copy

  • 强悍的Python读取大文件的解决方案

    Python 环境下文件的读取问题,请参见拙文 Python基础之文件读取的讲解 这是一道著名的 Python 面试题,考察的问题是,Python 读取大文件和一般规模的文件时的区别,也即哪些接口不适合读取大文件. 1. read() 接口的问题 f = open(filename, 'rb') f.read() 我们来读取 1 个 nginx 的日至文件,规模为 3Gb 大小.read() 方法执行的操作,是一次性全部读入内存,显然会造成: MemoryError ... 也即会发生内存溢出.

  • Python基础之文件读取的讲解

    with open(filename) as fp: dataMat = [] for line in fp.readlines(): # fp.readlines()返回一个list,list of strs # 也即line类型为`str` curLine = line.strip().split('\t') # 只有`str`类型才有strip()成员函数, # 在经过split()分割,得到list类型 # 也即curLine类型为list # curLine 仍然是由字符串构成的lis

  • Python标准库使用OrderedDict类的实例讲解

    目标:创建一个字典,记录几对python词语,使用OrderedDict类来写,并按顺序输出. 写完报错: [root@centos7 tmp]# python python_terms.py File "python_terms.py", line 9 from name,language in python_terms.items(): ^ SyntaxError: invalid syntax 代码如下: from collections import OrderedDict p

  • centos6.5安装python3.7.1之后无法使用pip的解决方案

    编译安装全是坑-- 第一遍装完无法使用pip,报错找不到ssl模块.各种报错: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. ... configure: error: Invalid --with-openssl value ... 结果各种捣鼓. 1.注意cetos6.5自带的openssl版本是1.0.1,需要升级到1.0.2

  • Python字符串逆序输出的实例讲解

    1.有时候我们可能想让字符串倒序输出,下面给出几种方法 方法一:通过索引的方法 >>> strA = "abcdegfgijlk" >>> strA[::-1] 'kljigfgedcba' 方法二:借组列表进行翻转 #coding=utf-8 strA = raw_input("请输入需要翻转的字符串:") order = [] for i in strA: order.append(i) order.reverse() #将列

  • Python从文件中读取数据的方法讲解

    编写了一个名为learning_python.txt的文件,内容如下: [root@centos7 tmp]# cat learning_python.txt In Python you can code; In Python you can learn object; In Python you can learn class. 要求:编写一个程序,它读取这个文件并打印三次. 1.第一次打印时读取整个文件: 2.第二次打印时遍历文件对象: 3.第三次打印时将各行存储在一个列表中,再在with代

  • 使用Python自动化破解自定义字体混淆信息的方法实例

    注意:本示例仅供学习参考- 混淆原理 出于某种原因,明文信息通过自定义字体进行渲染,达到混淆目的. 举个例子: 网页源码 <p>123</p> 在正常字体的渲染下,浏览者看到的是 123 这 3 个数字. 如果创建一种自定义字体,把 1 渲染成 5,那么浏览者看到的便是 523 这 3 个数字. 这样便达到混淆信息的效果,常见于对付爬虫之类的自动化工具. 破解方法 下载自定义字体文件(通常在 css @font-face 中找到),保存成 a.ttf 文件. 安装以下依赖项目 te

随机推荐