Dynamically allocate values
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
add a comment |
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
2
Can you add some sample data?
– jezrael
Nov 28 '18 at 6:28
add a comment |
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
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
python pandas
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
edited Nov 28 '18 at 6:51
answered Nov 28 '18 at 6:30
jezraeljezrael
347k25304379
347k25304379
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
Can you add some sample data?
– jezrael
Nov 28 '18 at 6:28