pandas获取数据集数据类型分布(更细粒度的分割)
方法一:使用pandas内置接口
在pandas中,获取数据类型有几个方法,以泰坦尼克号数据集为例,
1.拿到numerical数据类型
df.select_dtypes('number').columns
Index(['Survived', 'Pclass', 'Age', 'SibSp', 'Parch', 'Fare'], dtype='object')
2. 拿到categorical数据类型
df.select_dtypes('object').columns
Index(['Name', 'Sex', 'Ticket', 'Cabin', 'Embarked'], dtype='object')
还有
df.select_dtypes('category').columns
Index([], dtype='object')
方法二:pandas_profiling输出分析
以泰坦尼克号数据集为例,如果只是康康然后疯狂复制粘贴的话那没啥,但是如果想全流程自动化,就要把ProfileReport的结果用到接下来的数据处理中,不妨把结果输出到json文件。
from pandas_profiling import ProfileReport
import pandas as pddf = pd.read_csv('train.csv',index_col=['PassengerId'])
report = ProfileReport(df,dark_mode = True,explorative=True)report.to_file('result.json')
在这个json文件中,有如下的结构,取自己需要的统计即可,但是有亿点麻烦(doge)
方法三:自己动手写函数(相当于又把数据集分析了一遍doge)
def cols_spliting(df:pd.DataFrame, cardinality = 10, high_missing_per= 0.7, drop_high_missing = True):assert len(df.index) != 0binary_categorical_cols = []thin_categorical_cols = []uniform_categorical_cols = []categorical_cols = []numerical_cols = []other_cols = []high_missing_cols = []small_missing_cols = []missing_cols = []count = df.shape[0]for col in df.columns:unique = df[col].nunique()dtype = df[col].dtypemissing_count = df[col].isnull().sum() per = missing_count/count# type determineif unique <= 2 and dtype == 'object':binary_categorical_cols.append(col)categorical_cols.append(col)elif unique > 2 and unique <= cardinality and dtype == 'object':thin_categorical_cols.append(col)categorical_cols.append(col)elif unique > cardinality and dtype == 'object':uniform_categorical_cols.append(col)categorical_cols.append(col)elif dtype in ['int64', 'float64']:numerical_cols.append(col)else:other_cols.append(col) # missing determineif per > 0 and per <= high_missing_per:small_missing_cols.append(col)missing_cols.append(col)elif per > high_missing_per:high_missing_cols.append(col)missing_cols.append(col)print('--------------col types---------------') print('categorical cols with 1-2 distinct values: ' + str(binary_categorical_cols))print('categorical cols with 3-{} distinct values: '.format(cardinality) + str(thin_categorical_cols))print('categorical cols with more than {} distinct values: '.format(cardinality) + str(uniform_categorical_cols))print('categorical cols: ' + str(categorical_cols))print('numerical cols : ' + str(numerical_cols))print('-------------missing cols-------------')# print('categorical cols with 2-3 distinct valuses: ' + str(other_cols))print('missing cols with more than {} :'.format(high_missing_per) + str(high_missing_cols))print('missing cols with less than {} :'.format(high_missing_per) + str(small_missing_cols))print('missing cols: ' + str(missing_cols))
以泰坦尼克号为例:
cols_spliting(df)
结果为
--------------col types---------------
categorical cols with 2-3 distinct values: ['Sex']
categorical cols with 3-10 distinct values: ['Pclass', 'SibSp', 'Parch', 'Embarked']
categorical cols with more than 10 distinct values: ['Name', 'Ticket', 'Cabin']
categorical cols: ['Pclass', 'Name', 'Sex', 'SibSp', 'Parch', 'Ticket', 'Cabin', 'Embarked']
numerical cols : ['Age', 'Fare']
-------------missing cols-------------
missing cols with more than 0.7 : ['Cabin']
missing cols with less than 0.7 : ['Age', 'Embarked']
missing cols: ['Age', 'Cabin', 'Embarked']
如果我们对输出的数据还要要求,让他更适用于机器学习,不妨康康这个
pandas数据集类型划分II
pandas获取数据集数据类型分布(更细粒度的分割)相关推荐
- pandas获取dataframe数据列的数据类型、获取dataframe每类数据类型数据列的个数、使用select_dtypes函数、include参数以及exclude参数按照数据类型筛选数据
pandas获取dataframe数据列的数据类型.获取dataframe每类数据类型数据列的个数.使用select_dtypes函数.include参数以及exclude参数按照数据类型筛选数据 目 ...
- python获取期货行情可视化_用Pandas获取商品期货价格并可视化
用Pandas获取商品期货价格并可视化 摘 要 1.用pandas从excel中读取数据: 2.用pandas进行数据清洗.整理: 3.用bokeh进行简单的可视化. 1.数据读取 本文主要是将获取 ...
- 数据类型(数据类型简介、数据类型的分类(简单数据类型,获取变量数据类型,数据类型转换))
目录 数据类型 数据类型简介 为什么需要数据类型 变量的数据类型 数据类型的分类 简单数据类型 获取变量数据类型 数据类型转换 数据类型 数据类型简介 为什么需要数据类型 变量的数据类型 ...
- python pandas读取csv文件指定行_python pandas获取csv指定行 列的操作方法
python pandas获取csv指定行 列的操作方法 pandas获取csv指定行,列 house_info = pd.read_csv('house_info.csv') 1:取行的操作: ho ...
- Python pandas 里面的数据类型坑,astype要慎用
背景 最近在项目处理数据时,对pandas里面的数据类型进行转换时(astype),产生了一些意外的情况,经过研究,对数据框里面的数据类型,又有了新的认识,分享出来供大家参考学习. 创建模拟数据 模拟 ...
- JavaScript获取变量数据类型
JavaScript前文回顾: 认识JavaScript到初体验 JavaScript 注释以及输入输出语句 JavaScript变量的使用.语法扩展.命名规范 JavaScript数据类型简介以及简 ...
- Pandas的DataFrame数据类型
Pandas的DataFrame数据类型 纵轴表示不同索引axis=0,横轴表示不同列axis=1 DataFrame类型创建 1.从二维ndarray对象创建 import pandas as pd ...
- pandas获取dataframe中索引值最大值所在的数据行(get dataframe row of max index value)
pandas获取dataframe中索引值最大值所在的数据行(get dataframe row of max index value) 目录 pandas获取dataframe中索引值最大值所在的数 ...
- Pandas获取数据列名称的列表(list):(column list)
Pandas获取数据列名称的列表(list):(column list) 目录 Pandas获取数据列名称的列表(list):(column list) #仿真数据
最新文章
- python面试基本题(你需要的)
- Google和Yahoo专家联手揭秘世界顶尖公司的技术内幕
- 在博客园添加Lisp(或其它)代码高亮
- CentOS系统Nginx配置免费https证书
- 多线程学习笔记-深入理解ThreadPoolExecutor
- 阿里云域名备案时产品类型
- JSP自定义标签_控制标签体是否执行
- free malloc
- Spring的Bean的初始化
- java内存图解_图解JAVA内存模型(JMM:JAVA Memory Model)
- html设置文字超过字数_css限制文字显示字数长度,超出部分自动用省略号显示,防止溢出到第二行...
- 使用ros发布UVC相机和串口IMU数据
- 想尝试转前端,但不知道从何学起
- linux源代码剖析之include-asm
- php的原子操作,php实现含有redis命令的原子操作
- 怎么将php文件改成web的servlet文件_遇到喜欢的网站怎么才能高效收藏整理
- svn如何退回软件版本_SVN版本控制工具的使用
- 使用Composer部署laravel框架
- 计算机桌面图标任意排列,教你win7电脑桌面图标怎么随意摆放
- 孤独的根号三 (Missing NUMBER)