Pandas split gender column to two columns












2















I have the following table:
https://ibb.co/DMGCgD2



I want to split (Property Type) to two column count_Single_Family and count_Multi_Family in each zip code:



I used this code but I don't know what to do next:



b=Combineddf[Combineddf['Property Type']=='Multi-Family'].count()
a=Combineddf[Combineddf['Property Type']=='Single Family'].count()



I can do this with sql but I keep trying to get it to work with pandas but no luck. Thank you for the help.
Edit*
Input:



    import pandas as pd
input= pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002],
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Property Type': ['Multi-Family', 'Multi-Family', 'Single Family','Single Family','Single Family','Multi-Family']
})
input


Desired output:



    out = pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002], 
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Multi-Family': [2, 2, 2,1,1,1],
'Single Family': [1, 1, 1,2,2,2],

})
out









share|improve this question




















  • 1





    Use groupby.count

    – Ken Dekalb
    Nov 25 '18 at 20:38






  • 1





    @PeterLeimbigler this is from csv file i fixed the link

    – mango90001
    Nov 25 '18 at 20:55
















2















I have the following table:
https://ibb.co/DMGCgD2



I want to split (Property Type) to two column count_Single_Family and count_Multi_Family in each zip code:



I used this code but I don't know what to do next:



b=Combineddf[Combineddf['Property Type']=='Multi-Family'].count()
a=Combineddf[Combineddf['Property Type']=='Single Family'].count()



I can do this with sql but I keep trying to get it to work with pandas but no luck. Thank you for the help.
Edit*
Input:



    import pandas as pd
input= pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002],
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Property Type': ['Multi-Family', 'Multi-Family', 'Single Family','Single Family','Single Family','Multi-Family']
})
input


Desired output:



    out = pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002], 
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Multi-Family': [2, 2, 2,1,1,1],
'Single Family': [1, 1, 1,2,2,2],

})
out









share|improve this question




















  • 1





    Use groupby.count

    – Ken Dekalb
    Nov 25 '18 at 20:38






  • 1





    @PeterLeimbigler this is from csv file i fixed the link

    – mango90001
    Nov 25 '18 at 20:55














2












2








2








I have the following table:
https://ibb.co/DMGCgD2



I want to split (Property Type) to two column count_Single_Family and count_Multi_Family in each zip code:



I used this code but I don't know what to do next:



b=Combineddf[Combineddf['Property Type']=='Multi-Family'].count()
a=Combineddf[Combineddf['Property Type']=='Single Family'].count()



I can do this with sql but I keep trying to get it to work with pandas but no luck. Thank you for the help.
Edit*
Input:



    import pandas as pd
input= pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002],
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Property Type': ['Multi-Family', 'Multi-Family', 'Single Family','Single Family','Single Family','Multi-Family']
})
input


Desired output:



    out = pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002], 
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Multi-Family': [2, 2, 2,1,1,1],
'Single Family': [1, 1, 1,2,2,2],

})
out









share|improve this question
















I have the following table:
https://ibb.co/DMGCgD2



I want to split (Property Type) to two column count_Single_Family and count_Multi_Family in each zip code:



I used this code but I don't know what to do next:



b=Combineddf[Combineddf['Property Type']=='Multi-Family'].count()
a=Combineddf[Combineddf['Property Type']=='Single Family'].count()



I can do this with sql but I keep trying to get it to work with pandas but no luck. Thank you for the help.
Edit*
Input:



    import pandas as pd
input= pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002],
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Property Type': ['Multi-Family', 'Multi-Family', 'Single Family','Single Family','Single Family','Multi-Family']
})
input


Desired output:



    out = pd.DataFrame({'Zip Code': [90001, 90001, 90001,90002,90002,90002], 
'Total Males': [28468, 28468, 28468,43533,43533,43533],
'Total Female': [32135, 32135, 32135,54354,54354,54354],
'Multi-Family': [2, 2, 2,1,1,1],
'Single Family': [1, 1, 1,2,2,2],

})
out






python pandas split pandas-groupby






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 16:39









jpp

102k2165115




102k2165115










asked Nov 25 '18 at 20:31









mango90001mango90001

235




235








  • 1





    Use groupby.count

    – Ken Dekalb
    Nov 25 '18 at 20:38






  • 1





    @PeterLeimbigler this is from csv file i fixed the link

    – mango90001
    Nov 25 '18 at 20:55














  • 1





    Use groupby.count

    – Ken Dekalb
    Nov 25 '18 at 20:38






  • 1





    @PeterLeimbigler this is from csv file i fixed the link

    – mango90001
    Nov 25 '18 at 20:55








1




1





Use groupby.count

– Ken Dekalb
Nov 25 '18 at 20:38





Use groupby.count

– Ken Dekalb
Nov 25 '18 at 20:38




1




1





@PeterLeimbigler this is from csv file i fixed the link

– mango90001
Nov 25 '18 at 20:55





@PeterLeimbigler this is from csv file i fixed the link

– mango90001
Nov 25 '18 at 20:55












3 Answers
3






active

oldest

votes


















1















pd.crosstab + merge



Pandas has a method to cross-tabulate based on an index and values. You then need only merge these results with your original dataframe.



df_cross = pd.crosstab(df['Zip Code'], df['Property Type'])

res = df.merge(df_cross, left_on='Zip Code', right_index=True)

print(res)

Zip Code Total Males Total Female Property Type Multi-Family
0 90001 28468 32135 Multi-Family 2
1 90001 28468 32135 Multi-Family 2
2 90001 28468 32135 Single Family 2
3 90002 43533 54354 Single Family 1
4 90002 43533 54354 Single Family 1
5 90002 43533 54354 Multi-Family 1

Single Family
0 1
1 1
2 1
3 2
4 2
5 2





share|improve this answer































    0














    This seems to yield the right solution:



    v = input.groupby(['Zip Code','Property Type']).count().reset_index().pivot(index='Zip Code',columns = 'Property Type',values = 'Total Males')

    pd.merge(input,v, how = 'inner', left_on = 'Zip Code', right_on ='Zip Code').drop('Property Type',axis = 1)





    share|improve this answer
























    • the merge code giving me this KeyError: 'Zip Code'

      – mango90001
      Nov 25 '18 at 21:55













    • Works here on your input code.

      – Christian Sloper
      Nov 25 '18 at 21:56











    • how does v.columns look for you?

      – Christian Sloper
      Nov 25 '18 at 21:58



















    0














    Another solution:



    (pd.concat([input.set_index('Zip Code'), 
    input.groupby('Zip Code')['Property Type']
    .value_counts()
    .unstack()], axis=1)
    ).reset_index()

    Zip Code Total Males Total Female Property Type Multi-Family Single Family
    0 90001 28468 32135 Multi-Family 2 1
    1 90001 28468 32135 Multi-Family 2 1
    2 90001 28468 32135 Single Family 2 1
    3 90002 43533 54354 Single Family 1 2
    4 90002 43533 54354 Single Family 1 2
    5 90002 43533 54354 Multi-Family 1 2





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471647%2fpandas-split-gender-column-to-two-columns%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1















      pd.crosstab + merge



      Pandas has a method to cross-tabulate based on an index and values. You then need only merge these results with your original dataframe.



      df_cross = pd.crosstab(df['Zip Code'], df['Property Type'])

      res = df.merge(df_cross, left_on='Zip Code', right_index=True)

      print(res)

      Zip Code Total Males Total Female Property Type Multi-Family
      0 90001 28468 32135 Multi-Family 2
      1 90001 28468 32135 Multi-Family 2
      2 90001 28468 32135 Single Family 2
      3 90002 43533 54354 Single Family 1
      4 90002 43533 54354 Single Family 1
      5 90002 43533 54354 Multi-Family 1

      Single Family
      0 1
      1 1
      2 1
      3 2
      4 2
      5 2





      share|improve this answer




























        1















        pd.crosstab + merge



        Pandas has a method to cross-tabulate based on an index and values. You then need only merge these results with your original dataframe.



        df_cross = pd.crosstab(df['Zip Code'], df['Property Type'])

        res = df.merge(df_cross, left_on='Zip Code', right_index=True)

        print(res)

        Zip Code Total Males Total Female Property Type Multi-Family
        0 90001 28468 32135 Multi-Family 2
        1 90001 28468 32135 Multi-Family 2
        2 90001 28468 32135 Single Family 2
        3 90002 43533 54354 Single Family 1
        4 90002 43533 54354 Single Family 1
        5 90002 43533 54354 Multi-Family 1

        Single Family
        0 1
        1 1
        2 1
        3 2
        4 2
        5 2





        share|improve this answer


























          1












          1








          1








          pd.crosstab + merge



          Pandas has a method to cross-tabulate based on an index and values. You then need only merge these results with your original dataframe.



          df_cross = pd.crosstab(df['Zip Code'], df['Property Type'])

          res = df.merge(df_cross, left_on='Zip Code', right_index=True)

          print(res)

          Zip Code Total Males Total Female Property Type Multi-Family
          0 90001 28468 32135 Multi-Family 2
          1 90001 28468 32135 Multi-Family 2
          2 90001 28468 32135 Single Family 2
          3 90002 43533 54354 Single Family 1
          4 90002 43533 54354 Single Family 1
          5 90002 43533 54354 Multi-Family 1

          Single Family
          0 1
          1 1
          2 1
          3 2
          4 2
          5 2





          share|improve this answer














          pd.crosstab + merge



          Pandas has a method to cross-tabulate based on an index and values. You then need only merge these results with your original dataframe.



          df_cross = pd.crosstab(df['Zip Code'], df['Property Type'])

          res = df.merge(df_cross, left_on='Zip Code', right_index=True)

          print(res)

          Zip Code Total Males Total Female Property Type Multi-Family
          0 90001 28468 32135 Multi-Family 2
          1 90001 28468 32135 Multi-Family 2
          2 90001 28468 32135 Single Family 2
          3 90002 43533 54354 Single Family 1
          4 90002 43533 54354 Single Family 1
          5 90002 43533 54354 Multi-Family 1

          Single Family
          0 1
          1 1
          2 1
          3 2
          4 2
          5 2






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 25 '18 at 22:02









          jppjpp

          102k2165115




          102k2165115

























              0














              This seems to yield the right solution:



              v = input.groupby(['Zip Code','Property Type']).count().reset_index().pivot(index='Zip Code',columns = 'Property Type',values = 'Total Males')

              pd.merge(input,v, how = 'inner', left_on = 'Zip Code', right_on ='Zip Code').drop('Property Type',axis = 1)





              share|improve this answer
























              • the merge code giving me this KeyError: 'Zip Code'

                – mango90001
                Nov 25 '18 at 21:55













              • Works here on your input code.

                – Christian Sloper
                Nov 25 '18 at 21:56











              • how does v.columns look for you?

                – Christian Sloper
                Nov 25 '18 at 21:58
















              0














              This seems to yield the right solution:



              v = input.groupby(['Zip Code','Property Type']).count().reset_index().pivot(index='Zip Code',columns = 'Property Type',values = 'Total Males')

              pd.merge(input,v, how = 'inner', left_on = 'Zip Code', right_on ='Zip Code').drop('Property Type',axis = 1)





              share|improve this answer
























              • the merge code giving me this KeyError: 'Zip Code'

                – mango90001
                Nov 25 '18 at 21:55













              • Works here on your input code.

                – Christian Sloper
                Nov 25 '18 at 21:56











              • how does v.columns look for you?

                – Christian Sloper
                Nov 25 '18 at 21:58














              0












              0








              0







              This seems to yield the right solution:



              v = input.groupby(['Zip Code','Property Type']).count().reset_index().pivot(index='Zip Code',columns = 'Property Type',values = 'Total Males')

              pd.merge(input,v, how = 'inner', left_on = 'Zip Code', right_on ='Zip Code').drop('Property Type',axis = 1)





              share|improve this answer













              This seems to yield the right solution:



              v = input.groupby(['Zip Code','Property Type']).count().reset_index().pivot(index='Zip Code',columns = 'Property Type',values = 'Total Males')

              pd.merge(input,v, how = 'inner', left_on = 'Zip Code', right_on ='Zip Code').drop('Property Type',axis = 1)






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 25 '18 at 21:40









              Christian SloperChristian Sloper

              1,815416




              1,815416













              • the merge code giving me this KeyError: 'Zip Code'

                – mango90001
                Nov 25 '18 at 21:55













              • Works here on your input code.

                – Christian Sloper
                Nov 25 '18 at 21:56











              • how does v.columns look for you?

                – Christian Sloper
                Nov 25 '18 at 21:58



















              • the merge code giving me this KeyError: 'Zip Code'

                – mango90001
                Nov 25 '18 at 21:55













              • Works here on your input code.

                – Christian Sloper
                Nov 25 '18 at 21:56











              • how does v.columns look for you?

                – Christian Sloper
                Nov 25 '18 at 21:58

















              the merge code giving me this KeyError: 'Zip Code'

              – mango90001
              Nov 25 '18 at 21:55







              the merge code giving me this KeyError: 'Zip Code'

              – mango90001
              Nov 25 '18 at 21:55















              Works here on your input code.

              – Christian Sloper
              Nov 25 '18 at 21:56





              Works here on your input code.

              – Christian Sloper
              Nov 25 '18 at 21:56













              how does v.columns look for you?

              – Christian Sloper
              Nov 25 '18 at 21:58





              how does v.columns look for you?

              – Christian Sloper
              Nov 25 '18 at 21:58











              0














              Another solution:



              (pd.concat([input.set_index('Zip Code'), 
              input.groupby('Zip Code')['Property Type']
              .value_counts()
              .unstack()], axis=1)
              ).reset_index()

              Zip Code Total Males Total Female Property Type Multi-Family Single Family
              0 90001 28468 32135 Multi-Family 2 1
              1 90001 28468 32135 Multi-Family 2 1
              2 90001 28468 32135 Single Family 2 1
              3 90002 43533 54354 Single Family 1 2
              4 90002 43533 54354 Single Family 1 2
              5 90002 43533 54354 Multi-Family 1 2





              share|improve this answer






























                0














                Another solution:



                (pd.concat([input.set_index('Zip Code'), 
                input.groupby('Zip Code')['Property Type']
                .value_counts()
                .unstack()], axis=1)
                ).reset_index()

                Zip Code Total Males Total Female Property Type Multi-Family Single Family
                0 90001 28468 32135 Multi-Family 2 1
                1 90001 28468 32135 Multi-Family 2 1
                2 90001 28468 32135 Single Family 2 1
                3 90002 43533 54354 Single Family 1 2
                4 90002 43533 54354 Single Family 1 2
                5 90002 43533 54354 Multi-Family 1 2





                share|improve this answer




























                  0












                  0








                  0







                  Another solution:



                  (pd.concat([input.set_index('Zip Code'), 
                  input.groupby('Zip Code')['Property Type']
                  .value_counts()
                  .unstack()], axis=1)
                  ).reset_index()

                  Zip Code Total Males Total Female Property Type Multi-Family Single Family
                  0 90001 28468 32135 Multi-Family 2 1
                  1 90001 28468 32135 Multi-Family 2 1
                  2 90001 28468 32135 Single Family 2 1
                  3 90002 43533 54354 Single Family 1 2
                  4 90002 43533 54354 Single Family 1 2
                  5 90002 43533 54354 Multi-Family 1 2





                  share|improve this answer















                  Another solution:



                  (pd.concat([input.set_index('Zip Code'), 
                  input.groupby('Zip Code')['Property Type']
                  .value_counts()
                  .unstack()], axis=1)
                  ).reset_index()

                  Zip Code Total Males Total Female Property Type Multi-Family Single Family
                  0 90001 28468 32135 Multi-Family 2 1
                  1 90001 28468 32135 Multi-Family 2 1
                  2 90001 28468 32135 Single Family 2 1
                  3 90002 43533 54354 Single Family 1 2
                  4 90002 43533 54354 Single Family 1 2
                  5 90002 43533 54354 Multi-Family 1 2






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 25 '18 at 21:51

























                  answered Nov 25 '18 at 21:41









                  Peter LeimbiglerPeter Leimbigler

                  4,6631416




                  4,6631416






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471647%2fpandas-split-gender-column-to-two-columns%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Ottavio Pratesi

                      Tricia Helfer

                      15 giugno