Dynamically allocate values












2















I have a dataframe with columns like Name, cash, date. In the dataframe b I want to fill the xnpv values dynamically



def xnpv(rate, values, dates):
if rate <= -1.0:
return float('inf')
d0 = dates.min() # or min(dates)
return sum([ vi / (1.0 + rate)**((di - d0).days / 365.0) for vi, di in zip(values, dates)])

for cl in range(2,ctr_max+1,1):
grouped = b.groupby('Name')
b["XNPV"+str(cl)]=grouped.apply(lambda x: xnpv(0.1,
x[str(cl)+"cash"], x['Value Date']))


With the above code I want to dynamically fill the values like xnpv1, xnpv2,xnpv3 with the values 1cash, 2cash, 3cash. The result is coming to be NaN with the above code , but it do generate column xnpv1, xnpv2, xnpv3 but with NaN values. How can i solve this?










share|improve this question




















  • 2





    Can you add some sample data?

    – jezrael
    Nov 28 '18 at 6:28
















2















I have a dataframe with columns like Name, cash, date. In the dataframe b I want to fill the xnpv values dynamically



def xnpv(rate, values, dates):
if rate <= -1.0:
return float('inf')
d0 = dates.min() # or min(dates)
return sum([ vi / (1.0 + rate)**((di - d0).days / 365.0) for vi, di in zip(values, dates)])

for cl in range(2,ctr_max+1,1):
grouped = b.groupby('Name')
b["XNPV"+str(cl)]=grouped.apply(lambda x: xnpv(0.1,
x[str(cl)+"cash"], x['Value Date']))


With the above code I want to dynamically fill the values like xnpv1, xnpv2,xnpv3 with the values 1cash, 2cash, 3cash. The result is coming to be NaN with the above code , but it do generate column xnpv1, xnpv2, xnpv3 but with NaN values. How can i solve this?










share|improve this question




















  • 2





    Can you add some sample data?

    – jezrael
    Nov 28 '18 at 6:28














2












2








2








I have a dataframe with columns like Name, cash, date. In the dataframe b I want to fill the xnpv values dynamically



def xnpv(rate, values, dates):
if rate <= -1.0:
return float('inf')
d0 = dates.min() # or min(dates)
return sum([ vi / (1.0 + rate)**((di - d0).days / 365.0) for vi, di in zip(values, dates)])

for cl in range(2,ctr_max+1,1):
grouped = b.groupby('Name')
b["XNPV"+str(cl)]=grouped.apply(lambda x: xnpv(0.1,
x[str(cl)+"cash"], x['Value Date']))


With the above code I want to dynamically fill the values like xnpv1, xnpv2,xnpv3 with the values 1cash, 2cash, 3cash. The result is coming to be NaN with the above code , but it do generate column xnpv1, xnpv2, xnpv3 but with NaN values. How can i solve this?










share|improve this question
















I have a dataframe with columns like Name, cash, date. In the dataframe b I want to fill the xnpv values dynamically



def xnpv(rate, values, dates):
if rate <= -1.0:
return float('inf')
d0 = dates.min() # or min(dates)
return sum([ vi / (1.0 + rate)**((di - d0).days / 365.0) for vi, di in zip(values, dates)])

for cl in range(2,ctr_max+1,1):
grouped = b.groupby('Name')
b["XNPV"+str(cl)]=grouped.apply(lambda x: xnpv(0.1,
x[str(cl)+"cash"], x['Value Date']))


With the above code I want to dynamically fill the values like xnpv1, xnpv2,xnpv3 with the values 1cash, 2cash, 3cash. The result is coming to be NaN with the above code , but it do generate column xnpv1, xnpv2, xnpv3 but with NaN values. How can i solve this?







python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 6:31







Chetan P

















asked Nov 28 '18 at 6:18









Chetan PChetan P

7010




7010








  • 2





    Can you add some sample data?

    – jezrael
    Nov 28 '18 at 6:28














  • 2





    Can you add some sample data?

    – jezrael
    Nov 28 '18 at 6:28








2




2





Can you add some sample data?

– jezrael
Nov 28 '18 at 6:28





Can you add some sample data?

– jezrael
Nov 28 '18 at 6:28












1 Answer
1






active

oldest

votes


















0














I believe you need custom function:



b = pd.DataFrame({"Name":['a','a','a','a','b','b','c','c'],
"2cash":[1,1,3,4,1,2,4,5],
"3cash":[4,5,3,2,4,5,7,9],
"4cash":[1,1,2,4,5,1,3,4],
"Value Date":['2017-01-01','2017-02-01','2017-03-01','2017-04-01',
'2017-01-01','2017-02-01','2017-03-01','2017-04-01']
})
b["Value Date"] = pd.to_datetime(b["Value Date"])




def xnpv(rate, values, dates):
if rate <= -1.0:
return float('inf')
d0 = dates.min() # or min(dates)
return sum([ vi / (1.0 + rate)**((di - d0).days/ 365.0) for vi, di in zip(values, dates)])

ctr_max = 4
def f(x):
for cl in range(2,ctr_max+1,1):
x["XNPV{}".format(cl)] = xnpv(0.1, x["{}cash".format(cl)], x['Value Date'])
return x

df = b.groupby('Name').apply(f)
print (df)
Name 2cash 3cash 4cash Value Date XNPV2 XNPV3 XNPV4
0 a 1 4 1 2017-01-01 8.853165 13.867370 7.868453
1 a 1 5 1 2017-02-01 8.853165 13.867370 7.868453
2 a 3 3 2 2017-03-01 8.853165 13.867370 7.868453
3 a 4 2 4 2017-04-01 8.853165 13.867370 7.868453
4 b 1 4 5 2017-01-01 2.983876 8.959689 5.991938
5 b 2 5 1 2017-02-01 2.983876 8.959689 5.991938
6 c 4 7 3 2017-03-01 8.959689 15.927441 6.967751
7 c 5 9 4 2017-04-01 8.959689 15.927441 6.967751





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%2f53513255%2fdynamically-allocate-values%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I believe you need custom function:



    b = pd.DataFrame({"Name":['a','a','a','a','b','b','c','c'],
    "2cash":[1,1,3,4,1,2,4,5],
    "3cash":[4,5,3,2,4,5,7,9],
    "4cash":[1,1,2,4,5,1,3,4],
    "Value Date":['2017-01-01','2017-02-01','2017-03-01','2017-04-01',
    '2017-01-01','2017-02-01','2017-03-01','2017-04-01']
    })
    b["Value Date"] = pd.to_datetime(b["Value Date"])




    def xnpv(rate, values, dates):
    if rate <= -1.0:
    return float('inf')
    d0 = dates.min() # or min(dates)
    return sum([ vi / (1.0 + rate)**((di - d0).days/ 365.0) for vi, di in zip(values, dates)])

    ctr_max = 4
    def f(x):
    for cl in range(2,ctr_max+1,1):
    x["XNPV{}".format(cl)] = xnpv(0.1, x["{}cash".format(cl)], x['Value Date'])
    return x

    df = b.groupby('Name').apply(f)
    print (df)
    Name 2cash 3cash 4cash Value Date XNPV2 XNPV3 XNPV4
    0 a 1 4 1 2017-01-01 8.853165 13.867370 7.868453
    1 a 1 5 1 2017-02-01 8.853165 13.867370 7.868453
    2 a 3 3 2 2017-03-01 8.853165 13.867370 7.868453
    3 a 4 2 4 2017-04-01 8.853165 13.867370 7.868453
    4 b 1 4 5 2017-01-01 2.983876 8.959689 5.991938
    5 b 2 5 1 2017-02-01 2.983876 8.959689 5.991938
    6 c 4 7 3 2017-03-01 8.959689 15.927441 6.967751
    7 c 5 9 4 2017-04-01 8.959689 15.927441 6.967751





    share|improve this answer






























      0














      I believe you need custom function:



      b = pd.DataFrame({"Name":['a','a','a','a','b','b','c','c'],
      "2cash":[1,1,3,4,1,2,4,5],
      "3cash":[4,5,3,2,4,5,7,9],
      "4cash":[1,1,2,4,5,1,3,4],
      "Value Date":['2017-01-01','2017-02-01','2017-03-01','2017-04-01',
      '2017-01-01','2017-02-01','2017-03-01','2017-04-01']
      })
      b["Value Date"] = pd.to_datetime(b["Value Date"])




      def xnpv(rate, values, dates):
      if rate <= -1.0:
      return float('inf')
      d0 = dates.min() # or min(dates)
      return sum([ vi / (1.0 + rate)**((di - d0).days/ 365.0) for vi, di in zip(values, dates)])

      ctr_max = 4
      def f(x):
      for cl in range(2,ctr_max+1,1):
      x["XNPV{}".format(cl)] = xnpv(0.1, x["{}cash".format(cl)], x['Value Date'])
      return x

      df = b.groupby('Name').apply(f)
      print (df)
      Name 2cash 3cash 4cash Value Date XNPV2 XNPV3 XNPV4
      0 a 1 4 1 2017-01-01 8.853165 13.867370 7.868453
      1 a 1 5 1 2017-02-01 8.853165 13.867370 7.868453
      2 a 3 3 2 2017-03-01 8.853165 13.867370 7.868453
      3 a 4 2 4 2017-04-01 8.853165 13.867370 7.868453
      4 b 1 4 5 2017-01-01 2.983876 8.959689 5.991938
      5 b 2 5 1 2017-02-01 2.983876 8.959689 5.991938
      6 c 4 7 3 2017-03-01 8.959689 15.927441 6.967751
      7 c 5 9 4 2017-04-01 8.959689 15.927441 6.967751





      share|improve this answer




























        0












        0








        0







        I believe you need custom function:



        b = pd.DataFrame({"Name":['a','a','a','a','b','b','c','c'],
        "2cash":[1,1,3,4,1,2,4,5],
        "3cash":[4,5,3,2,4,5,7,9],
        "4cash":[1,1,2,4,5,1,3,4],
        "Value Date":['2017-01-01','2017-02-01','2017-03-01','2017-04-01',
        '2017-01-01','2017-02-01','2017-03-01','2017-04-01']
        })
        b["Value Date"] = pd.to_datetime(b["Value Date"])




        def xnpv(rate, values, dates):
        if rate <= -1.0:
        return float('inf')
        d0 = dates.min() # or min(dates)
        return sum([ vi / (1.0 + rate)**((di - d0).days/ 365.0) for vi, di in zip(values, dates)])

        ctr_max = 4
        def f(x):
        for cl in range(2,ctr_max+1,1):
        x["XNPV{}".format(cl)] = xnpv(0.1, x["{}cash".format(cl)], x['Value Date'])
        return x

        df = b.groupby('Name').apply(f)
        print (df)
        Name 2cash 3cash 4cash Value Date XNPV2 XNPV3 XNPV4
        0 a 1 4 1 2017-01-01 8.853165 13.867370 7.868453
        1 a 1 5 1 2017-02-01 8.853165 13.867370 7.868453
        2 a 3 3 2 2017-03-01 8.853165 13.867370 7.868453
        3 a 4 2 4 2017-04-01 8.853165 13.867370 7.868453
        4 b 1 4 5 2017-01-01 2.983876 8.959689 5.991938
        5 b 2 5 1 2017-02-01 2.983876 8.959689 5.991938
        6 c 4 7 3 2017-03-01 8.959689 15.927441 6.967751
        7 c 5 9 4 2017-04-01 8.959689 15.927441 6.967751





        share|improve this answer















        I believe you need custom function:



        b = pd.DataFrame({"Name":['a','a','a','a','b','b','c','c'],
        "2cash":[1,1,3,4,1,2,4,5],
        "3cash":[4,5,3,2,4,5,7,9],
        "4cash":[1,1,2,4,5,1,3,4],
        "Value Date":['2017-01-01','2017-02-01','2017-03-01','2017-04-01',
        '2017-01-01','2017-02-01','2017-03-01','2017-04-01']
        })
        b["Value Date"] = pd.to_datetime(b["Value Date"])




        def xnpv(rate, values, dates):
        if rate <= -1.0:
        return float('inf')
        d0 = dates.min() # or min(dates)
        return sum([ vi / (1.0 + rate)**((di - d0).days/ 365.0) for vi, di in zip(values, dates)])

        ctr_max = 4
        def f(x):
        for cl in range(2,ctr_max+1,1):
        x["XNPV{}".format(cl)] = xnpv(0.1, x["{}cash".format(cl)], x['Value Date'])
        return x

        df = b.groupby('Name').apply(f)
        print (df)
        Name 2cash 3cash 4cash Value Date XNPV2 XNPV3 XNPV4
        0 a 1 4 1 2017-01-01 8.853165 13.867370 7.868453
        1 a 1 5 1 2017-02-01 8.853165 13.867370 7.868453
        2 a 3 3 2 2017-03-01 8.853165 13.867370 7.868453
        3 a 4 2 4 2017-04-01 8.853165 13.867370 7.868453
        4 b 1 4 5 2017-01-01 2.983876 8.959689 5.991938
        5 b 2 5 1 2017-02-01 2.983876 8.959689 5.991938
        6 c 4 7 3 2017-03-01 8.959689 15.927441 6.967751
        7 c 5 9 4 2017-04-01 8.959689 15.927441 6.967751






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 28 '18 at 6:51

























        answered Nov 28 '18 at 6:30









        jezraeljezrael

        347k25304379




        347k25304379
































            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%2f53513255%2fdynamically-allocate-values%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

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)