pandas数据集的端到端处理

yipeiwu_com5年前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中Apriori算法实现讲解

python中Apriori算法实现讲解

本文主要给大家讲解了Apriori算法的基础知识以及Apriori算法python中的实现过程,以下是所有内容: 1. Apriori算法简介 Apriori算法是挖掘布尔关联规则频繁项...

Python字符串替换实例分析

本文实例讲述了Python字符串替换的方法。分享给大家供大家参考。具体如下: 单个字符替换 s = 'abcd' a = ["a", "b", "c"] b = ["c", "d",...

python画柱状图--不同颜色并显示数值的方法

python画柱状图--不同颜色并显示数值的方法

用python画柱状图容易,但是如何对不同柱子使用不同颜色呢?同时在柱子顶端显示精确数值? 主要用的方法为: atplotlib.pyplot.bar(left, height, wid...

python执行系统命令后获取返回值的几种方式集合

第一种情况 os.system('ps aux') 执行系统命令,没有返回值 第二种情况 result = os.popen('ps aux') res = resu...

python编写弹球游戏的实现代码

 弹球游戏: from tkinter import * import time import random tk=Tk() #创建一个界面 tk...