pandas获取数据集数据类型分布(更细粒度的分割)

11 篇文章 0 订阅
2 篇文章 0 订阅

方法一:使用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 pd

df = 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) != 0
																
    binary_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].dtype
        
        missing_count = df[col].isnull().sum()          
        per = missing_count/count
        
        # type determine
        if 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 determine
        if  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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值