以matplotlib中每个类别中总值的比例绘制每个类别的缺失值

2024-06-08 16:57:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我在数据集中有两列,一列是国家,另一列是性别,性别有一些NaN或N/A值。在熊猫身上看起来是这样的:

import pandas as pd    
Country = ['United Kingdom', 'Bosnia and Herzegovina', 'Thailand', 'United States', 'Ukraine', 'Canada', 'Ukraine', 'India', 'New Zealand', 'India', 'Antigua and Barbuda', 'Canada', 'United States', 'Germany', 'India', 'United Kingdom', 'Australia', 'Russian Federation', 'Brazil', 'Lithuania']
Gender = ['Man', 'Man', 'Man', 'Man', 'Man', 'Man', 'Man', 'Man', 'Man', nan, 'Man', 'Woman', 'Man', 'Man', 'Man', 'Man', 'Man', 'Man', 'Man', 'Man']

我想在一个国家类别中,将缺失的价值标绘为所有性别价值的一个比例。我首先使用了groupby on Country专栏,该专栏只提供了一个国家内的性别总数,将缺失值绘制为一个国家内总性别值的比例的最佳方法是什么:

编辑:在评论中回答此问题,请查看解决方案,如果您认为解决方案可能更好,请在解决方案评论下进行评论


Tags: and评论国家解决方案country比例unitedkingdom
1条回答
网友
1楼 · 发布于 2024-06-08 16:57:11

嗨,这里是我为它创建了一个解决方案

  • 首先,我创建了一个类似的数据框架,它的所有值都是Gender=NaN
  • 然后我用1代替了NaN
  • 我按国家对这个数据框进行了分组,并对性别列计数进行了汇总
  • 然后在原始数据帧上,我再次用1填充NAN(这可以在第一步中完成)
  • 然后按国家对该数据框进行分组,并对性别列进行汇总
  • 我在轴1上连接了这两个数据帧
  • 我创建了一个列,该列采用dataframe 1性别计数(count of Gender=NaN/国家),乘以100除以第二个数据框性别计数
  • 然后我将这个新列绘制为条形图

enter image description here

    Country = ['United Kingdom', 'Bosnia and Herzegovina', 'Thailand', 'United States', 'Ukraine', 'Canada', 'Ukraine', 'India', 'New Zealand', 'India', 'Antigua and Barbuda', 'Canada', 'United States', 'Germany', 'India', 'United Kingdom', 'Australia', 'Russian Federation', 'Brazil', 'Lithuania']
    Gender = ['Man', 'Man', 'Man', 'Man', 'Man', 'Man', 'Man', 'Man', 'Man', nan, 'Man', 'Woman', 'Man', 'Man', 'Man', 'Man', 'Man', 'Man', 'Man', 'Man']
    survey = pd.DataFrame({ 'Country' :Country , 'Gender':Gender})
    null_gender["Gender"].fillna(1, inplace = True) # filling 1 instead of NaN for the count in next line of code
    freq = null_gender.groupby('Country')['Gender'].count().rename('NullCount') # Number of NaN in Gender Column per country

    survey["Gender"].fillna(1, inplace = True)
    freq_full =survey.groupby('Country')['Gender'].count().rename('Totalcount')
    total_freq = pd.concat([freq, freq_full], axis=1)
    total_freq = total_freq.dropna(how='any',axis=0) 
    total_freq['null_percent'] = total_freq.apply(lambda row: round((row.NullCount*100) /row.Totalcount, 2) , axis = 1) 
    total_freq.sort_values('null_percent', ascending=True, inplace=True)
    total_freq['null_percent'].plot(kind='barh', figsize=(10,25))

相关问题 更多 >