• <tfoot id='qmGcW'></tfoot>
      <bdo id='qmGcW'></bdo><ul id='qmGcW'></ul>
  • <small id='qmGcW'></small><noframes id='qmGcW'>

    <legend id='qmGcW'><style id='qmGcW'><dir id='qmGcW'><q id='qmGcW'></q></dir></style></legend>
      <i id='qmGcW'><tr id='qmGcW'><dt id='qmGcW'><q id='qmGcW'><span id='qmGcW'><b id='qmGcW'><form id='qmGcW'><ins id='qmGcW'></ins><ul id='qmGcW'></ul><sub id='qmGcW'></sub></form><legend id='qmGcW'></legend><bdo id='qmGcW'><pre id='qmGcW'><center id='qmGcW'></center></pre></bdo></b><th id='qmGcW'></th></span></q></dt></tr></i><div id='qmGcW'><tfoot id='qmGcW'></tfoot><dl id='qmGcW'><fieldset id='qmGcW'></fieldset></dl></div>

        如何在图表中绘制 pandas 的分组值

        How to plot pandas groupby values in a graph(如何在图表中绘制 pandas 的分组值)

          <legend id='TPfWP'><style id='TPfWP'><dir id='TPfWP'><q id='TPfWP'></q></dir></style></legend>
            <tbody id='TPfWP'></tbody>
          <tfoot id='TPfWP'></tfoot>

            <bdo id='TPfWP'></bdo><ul id='TPfWP'></ul>
          • <i id='TPfWP'><tr id='TPfWP'><dt id='TPfWP'><q id='TPfWP'><span id='TPfWP'><b id='TPfWP'><form id='TPfWP'><ins id='TPfWP'></ins><ul id='TPfWP'></ul><sub id='TPfWP'></sub></form><legend id='TPfWP'></legend><bdo id='TPfWP'><pre id='TPfWP'><center id='TPfWP'></center></pre></bdo></b><th id='TPfWP'></th></span></q></dt></tr></i><div id='TPfWP'><tfoot id='TPfWP'></tfoot><dl id='TPfWP'><fieldset id='TPfWP'></fieldset></dl></div>
              1. <small id='TPfWP'></small><noframes id='TPfWP'>

                  本文介绍了如何在图表中绘制 pandas 的分组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个CSV文件,其中包含性别和婚姻状况,以及一些类似下面的列。

                  Loan_ID,Gender,Married,Dependents,Education,Self_Employed,ApplicantIncome,CoapplicantIncome,LoanAmount,Loan_Amount_Term,Credit_History,Property_Area,Loan_Status
                  LP001002,Male,No,0,Graduate,No,5849,0,,360,1,Urban,Y
                  LP001003,Male,Yes,1,Graduate,No,4583,1508,128,360,1,Rural,N
                  LP001005,Male,Yes,0,Graduate,Yes,3000,0,66,360,1,Urban,Y
                  LP001006,Male,Yes,0,Not Graduate,No,2583,2358,120,360,1,Urban,Y
                  LP001008,Male,No,0,Graduate,No,6000,0,141,360,1,Urban,Y
                  LP001011,Male,Yes,2,Graduate,Yes,5417,4196,267,360,1,Urban,Y
                  

                  我想数一下没有。并在图表中显示,如下所示

                  下面是我使用的代码:

                  import csv
                  import pandas as pd
                  import numpy as np
                  import matplotlib.pyplot as plt
                  
                  if __name__ == '__main__':
                      x=[]
                      y=[]
                      df = pd.read_csv(
                          "/home/train.csv",usecols=[1,2]).dropna(subset=['Gender','Married'])  # Reading the dataset in a dataframe using Pandas
                      groups = df.groupby(['Gender','Married'])['Married'].apply(lambda x: x.count())
                      print(groups)
                  

                  在GROUP BY I之后,结果如下:

                  Gender  Married
                  Female  No          80
                          Yes         31
                  Male    No         130
                          Yes        357
                  

                  我想要以下图表

                  推荐答案

                  可以使用groupby+size,然后使用Series.plot.bar

                  Difference between count and size。

                  groups = df.groupby(['Gender','Married']).size()
                  groups.plot.bar()
                  

                  另一个解决方案是添加unstack进行整形或crosstab

                  print (df.groupby(['Gender','Married']).size().unstack(fill_value=0))
                  Married   No  Yes
                  Gender           
                  Female    80   31
                  Male     130  357
                  
                  df.groupby(['Gender','Married']).size().unstack(fill_value=0).plot.bar()
                  

                  或:

                  pd.crosstab(df['Gender'],df['Married']).plot.bar()
                  

                  这篇关于如何在图表中绘制 pandas 的分组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Is there a way of group by month in Pandas starting at specific day number?( pandas 有从特定日期开始的按月分组的方式吗?)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)

                    <tbody id='ubKk4'></tbody>
                      1. <i id='ubKk4'><tr id='ubKk4'><dt id='ubKk4'><q id='ubKk4'><span id='ubKk4'><b id='ubKk4'><form id='ubKk4'><ins id='ubKk4'></ins><ul id='ubKk4'></ul><sub id='ubKk4'></sub></form><legend id='ubKk4'></legend><bdo id='ubKk4'><pre id='ubKk4'><center id='ubKk4'></center></pre></bdo></b><th id='ubKk4'></th></span></q></dt></tr></i><div id='ubKk4'><tfoot id='ubKk4'></tfoot><dl id='ubKk4'><fieldset id='ubKk4'></fieldset></dl></div>

                        <small id='ubKk4'></small><noframes id='ubKk4'>

                      2. <tfoot id='ubKk4'></tfoot>
                        • <legend id='ubKk4'><style id='ubKk4'><dir id='ubKk4'><q id='ubKk4'></q></dir></style></legend>
                            <bdo id='ubKk4'></bdo><ul id='ubKk4'></ul>