Calculate pro rata ownership using Python Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to deal with SettingWithCopyWarning in Pandas?How to use groupby in pandas to calculate a percentage / proportion total based on a criteria in another columnCalling an external command in PythonWhat are metaclasses in Python?Is there a way to run Python on Android?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?

Did any compiler fully use 80-bit floating point?

How can a team of shapeshifters communicate?

Delete free apps from library

Test print coming out spongy

A term for a woman complaining about things/begging in a cute/childish way

Does the Mueller report show a conspiracy between Russia and the Trump Campaign?

Why are vacuum tubes still used in amateur radios?

Sally's older brother

Why does electrolysis of aqueous concentrated sodium bromide produce bromine at the anode?

Trying to understand entropy as a novice in thermodynamics

The test team as an enemy of development? And how can this be avoided?

Asymptotics question

What initially awakened the Balrog?

Random body shuffle every night—can we still function?

Project Euler #1 in C++

How to change the tick of the color bar legend to black

How do living politicians protect their readily obtainable signatures from misuse?

Google .dev domain strangely redirects to https

What is the origin of 落第?

What order were files/directories output in dir?

Should a wizard buy fine inks every time he want to copy spells into his spellbook?

Was Kant an Intuitionist about mathematical objects?

Putting class ranking in CV, but against dept guidelines

How much damage would a cupful of neutron star matter do to the Earth?



Calculate pro rata ownership using Python



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to deal with SettingWithCopyWarning in Pandas?How to use groupby in pandas to calculate a percentage / proportion total based on a criteria in another columnCalling an external command in PythonWhat are metaclasses in Python?Is there a way to run Python on Android?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am relatively new to Python so pardon my question if it's relatively basic (have not been able to find anything helpful online).



I have a dataframe that has 3 columns, Fund | Investor | Quantity, and need to insert a new column into my dataframe that calculates each investors' pro-rata ownership.



I assume looping through the dataframe is the best way to do this but am having issues.



enter image description here










share|improve this question
























  • Welcome! Could you please state some actual question? :)

    – Nico Albers
    Mar 8 at 22:36











  • and the first version was perfect, please don't include images of code! You can format it via the toolbar, there is one code button

    – Nico Albers
    Mar 8 at 22:39


















1















I am relatively new to Python so pardon my question if it's relatively basic (have not been able to find anything helpful online).



I have a dataframe that has 3 columns, Fund | Investor | Quantity, and need to insert a new column into my dataframe that calculates each investors' pro-rata ownership.



I assume looping through the dataframe is the best way to do this but am having issues.



enter image description here










share|improve this question
























  • Welcome! Could you please state some actual question? :)

    – Nico Albers
    Mar 8 at 22:36











  • and the first version was perfect, please don't include images of code! You can format it via the toolbar, there is one code button

    – Nico Albers
    Mar 8 at 22:39














1












1








1


1






I am relatively new to Python so pardon my question if it's relatively basic (have not been able to find anything helpful online).



I have a dataframe that has 3 columns, Fund | Investor | Quantity, and need to insert a new column into my dataframe that calculates each investors' pro-rata ownership.



I assume looping through the dataframe is the best way to do this but am having issues.



enter image description here










share|improve this question
















I am relatively new to Python so pardon my question if it's relatively basic (have not been able to find anything helpful online).



I have a dataframe that has 3 columns, Fund | Investor | Quantity, and need to insert a new column into my dataframe that calculates each investors' pro-rata ownership.



I assume looping through the dataframe is the best way to do this but am having issues.



enter image description here







python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 22:58









Zoe

13.8k85586




13.8k85586










asked Mar 8 at 22:34









jrassjrass

224




224












  • Welcome! Could you please state some actual question? :)

    – Nico Albers
    Mar 8 at 22:36











  • and the first version was perfect, please don't include images of code! You can format it via the toolbar, there is one code button

    – Nico Albers
    Mar 8 at 22:39


















  • Welcome! Could you please state some actual question? :)

    – Nico Albers
    Mar 8 at 22:36











  • and the first version was perfect, please don't include images of code! You can format it via the toolbar, there is one code button

    – Nico Albers
    Mar 8 at 22:39

















Welcome! Could you please state some actual question? :)

– Nico Albers
Mar 8 at 22:36





Welcome! Could you please state some actual question? :)

– Nico Albers
Mar 8 at 22:36













and the first version was perfect, please don't include images of code! You can format it via the toolbar, there is one code button

– Nico Albers
Mar 8 at 22:39






and the first version was perfect, please don't include images of code! You can format it via the toolbar, there is one code button

– Nico Albers
Mar 8 at 22:39













3 Answers
3






active

oldest

votes


















0














See this question.
Therefore you can group and apply:



In [1]: df = pd.DataFrame([
...: ['Fund 1','Investor A', 10],
...: ['Fund 1','Investor B', 20],
...: ['Fund 2','Investor A', 30],
...: ['Fund 2','Investor B', 40],
...: ['Fund 2','Investor C', 30],
...: ['Fund 3','Investor A', 50],
...: ['Fund 3','Investor B', 50],
...: ], columns=['Fund','Investor', 'Qty'])
...:

In [2]: df['wanted'] = df.groupby('Fund').Qty.apply(lambda x: x/x.sum())

In [3]: df
Out[3]:
Fund Investor Qty wanted
0 Fund 1 Investor A 10 0.333333
1 Fund 1 Investor B 20 0.666667
2 Fund 2 Investor A 30 0.300000
3 Fund 2 Investor B 40 0.400000
4 Fund 2 Investor C 30 0.300000
5 Fund 3 Investor A 50 0.500000
6 Fund 3 Investor B 50 0.500000


The last step towards the percentages will be easy for you.






share|improve this answer























  • Thank you for your answer and apologies for duplicating. This works as expected. I do get a message," 89: SettingWithCopyWarning", that i may need to look into

    – jrass
    Mar 8 at 23:31











  • You're welcome! For the warning see for example this. In case this doesn't help you feel free to open a new question :-)

    – Nico Albers
    Mar 9 at 8:49


















0














It would be very easy if you can post your code but for reference you can use like:



df['D'] = df['A'] + df['B'] + df['C']


try above way in your existing code. Let me know if it helps.






share|improve this answer






























    0














    try:



    df['percent'] = df['QTY'] / df.groupby('Fund')['QTY'].transform('sum') * 100





    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%2f55071945%2fcalculate-pro-rata-ownership-using-python%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









      0














      See this question.
      Therefore you can group and apply:



      In [1]: df = pd.DataFrame([
      ...: ['Fund 1','Investor A', 10],
      ...: ['Fund 1','Investor B', 20],
      ...: ['Fund 2','Investor A', 30],
      ...: ['Fund 2','Investor B', 40],
      ...: ['Fund 2','Investor C', 30],
      ...: ['Fund 3','Investor A', 50],
      ...: ['Fund 3','Investor B', 50],
      ...: ], columns=['Fund','Investor', 'Qty'])
      ...:

      In [2]: df['wanted'] = df.groupby('Fund').Qty.apply(lambda x: x/x.sum())

      In [3]: df
      Out[3]:
      Fund Investor Qty wanted
      0 Fund 1 Investor A 10 0.333333
      1 Fund 1 Investor B 20 0.666667
      2 Fund 2 Investor A 30 0.300000
      3 Fund 2 Investor B 40 0.400000
      4 Fund 2 Investor C 30 0.300000
      5 Fund 3 Investor A 50 0.500000
      6 Fund 3 Investor B 50 0.500000


      The last step towards the percentages will be easy for you.






      share|improve this answer























      • Thank you for your answer and apologies for duplicating. This works as expected. I do get a message," 89: SettingWithCopyWarning", that i may need to look into

        – jrass
        Mar 8 at 23:31











      • You're welcome! For the warning see for example this. In case this doesn't help you feel free to open a new question :-)

        – Nico Albers
        Mar 9 at 8:49















      0














      See this question.
      Therefore you can group and apply:



      In [1]: df = pd.DataFrame([
      ...: ['Fund 1','Investor A', 10],
      ...: ['Fund 1','Investor B', 20],
      ...: ['Fund 2','Investor A', 30],
      ...: ['Fund 2','Investor B', 40],
      ...: ['Fund 2','Investor C', 30],
      ...: ['Fund 3','Investor A', 50],
      ...: ['Fund 3','Investor B', 50],
      ...: ], columns=['Fund','Investor', 'Qty'])
      ...:

      In [2]: df['wanted'] = df.groupby('Fund').Qty.apply(lambda x: x/x.sum())

      In [3]: df
      Out[3]:
      Fund Investor Qty wanted
      0 Fund 1 Investor A 10 0.333333
      1 Fund 1 Investor B 20 0.666667
      2 Fund 2 Investor A 30 0.300000
      3 Fund 2 Investor B 40 0.400000
      4 Fund 2 Investor C 30 0.300000
      5 Fund 3 Investor A 50 0.500000
      6 Fund 3 Investor B 50 0.500000


      The last step towards the percentages will be easy for you.






      share|improve this answer























      • Thank you for your answer and apologies for duplicating. This works as expected. I do get a message," 89: SettingWithCopyWarning", that i may need to look into

        – jrass
        Mar 8 at 23:31











      • You're welcome! For the warning see for example this. In case this doesn't help you feel free to open a new question :-)

        – Nico Albers
        Mar 9 at 8:49













      0












      0








      0







      See this question.
      Therefore you can group and apply:



      In [1]: df = pd.DataFrame([
      ...: ['Fund 1','Investor A', 10],
      ...: ['Fund 1','Investor B', 20],
      ...: ['Fund 2','Investor A', 30],
      ...: ['Fund 2','Investor B', 40],
      ...: ['Fund 2','Investor C', 30],
      ...: ['Fund 3','Investor A', 50],
      ...: ['Fund 3','Investor B', 50],
      ...: ], columns=['Fund','Investor', 'Qty'])
      ...:

      In [2]: df['wanted'] = df.groupby('Fund').Qty.apply(lambda x: x/x.sum())

      In [3]: df
      Out[3]:
      Fund Investor Qty wanted
      0 Fund 1 Investor A 10 0.333333
      1 Fund 1 Investor B 20 0.666667
      2 Fund 2 Investor A 30 0.300000
      3 Fund 2 Investor B 40 0.400000
      4 Fund 2 Investor C 30 0.300000
      5 Fund 3 Investor A 50 0.500000
      6 Fund 3 Investor B 50 0.500000


      The last step towards the percentages will be easy for you.






      share|improve this answer













      See this question.
      Therefore you can group and apply:



      In [1]: df = pd.DataFrame([
      ...: ['Fund 1','Investor A', 10],
      ...: ['Fund 1','Investor B', 20],
      ...: ['Fund 2','Investor A', 30],
      ...: ['Fund 2','Investor B', 40],
      ...: ['Fund 2','Investor C', 30],
      ...: ['Fund 3','Investor A', 50],
      ...: ['Fund 3','Investor B', 50],
      ...: ], columns=['Fund','Investor', 'Qty'])
      ...:

      In [2]: df['wanted'] = df.groupby('Fund').Qty.apply(lambda x: x/x.sum())

      In [3]: df
      Out[3]:
      Fund Investor Qty wanted
      0 Fund 1 Investor A 10 0.333333
      1 Fund 1 Investor B 20 0.666667
      2 Fund 2 Investor A 30 0.300000
      3 Fund 2 Investor B 40 0.400000
      4 Fund 2 Investor C 30 0.300000
      5 Fund 3 Investor A 50 0.500000
      6 Fund 3 Investor B 50 0.500000


      The last step towards the percentages will be easy for you.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 8 at 23:00









      Nico AlbersNico Albers

      841722




      841722












      • Thank you for your answer and apologies for duplicating. This works as expected. I do get a message," 89: SettingWithCopyWarning", that i may need to look into

        – jrass
        Mar 8 at 23:31











      • You're welcome! For the warning see for example this. In case this doesn't help you feel free to open a new question :-)

        – Nico Albers
        Mar 9 at 8:49

















      • Thank you for your answer and apologies for duplicating. This works as expected. I do get a message," 89: SettingWithCopyWarning", that i may need to look into

        – jrass
        Mar 8 at 23:31











      • You're welcome! For the warning see for example this. In case this doesn't help you feel free to open a new question :-)

        – Nico Albers
        Mar 9 at 8:49
















      Thank you for your answer and apologies for duplicating. This works as expected. I do get a message," 89: SettingWithCopyWarning", that i may need to look into

      – jrass
      Mar 8 at 23:31





      Thank you for your answer and apologies for duplicating. This works as expected. I do get a message," 89: SettingWithCopyWarning", that i may need to look into

      – jrass
      Mar 8 at 23:31













      You're welcome! For the warning see for example this. In case this doesn't help you feel free to open a new question :-)

      – Nico Albers
      Mar 9 at 8:49





      You're welcome! For the warning see for example this. In case this doesn't help you feel free to open a new question :-)

      – Nico Albers
      Mar 9 at 8:49













      0














      It would be very easy if you can post your code but for reference you can use like:



      df['D'] = df['A'] + df['B'] + df['C']


      try above way in your existing code. Let me know if it helps.






      share|improve this answer



























        0














        It would be very easy if you can post your code but for reference you can use like:



        df['D'] = df['A'] + df['B'] + df['C']


        try above way in your existing code. Let me know if it helps.






        share|improve this answer

























          0












          0








          0







          It would be very easy if you can post your code but for reference you can use like:



          df['D'] = df['A'] + df['B'] + df['C']


          try above way in your existing code. Let me know if it helps.






          share|improve this answer













          It would be very easy if you can post your code but for reference you can use like:



          df['D'] = df['A'] + df['B'] + df['C']


          try above way in your existing code. Let me know if it helps.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 22:41









          Jitendra BanshpalJitendra Banshpal

          540414




          540414





















              0














              try:



              df['percent'] = df['QTY'] / df.groupby('Fund')['QTY'].transform('sum') * 100





              share|improve this answer



























                0














                try:



                df['percent'] = df['QTY'] / df.groupby('Fund')['QTY'].transform('sum') * 100





                share|improve this answer

























                  0












                  0








                  0







                  try:



                  df['percent'] = df['QTY'] / df.groupby('Fund')['QTY'].transform('sum') * 100





                  share|improve this answer













                  try:



                  df['percent'] = df['QTY'] / df.groupby('Fund')['QTY'].transform('sum') * 100






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 at 23:00









                  TerryTerry

                  571517




                  571517



























                      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%2f55071945%2fcalculate-pro-rata-ownership-using-python%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

                      1928 у кіно

                      Захаров Федір Захарович

                      Ель Греко