Conditional replacement in pandas for each row

Multi tool use
I am probably doing something very simple, but I cant figure out the trick there.
I have a dataframe, and I want to replace the values in a particular column that exceed a value from zero with some random value. I had thought this was a way of achieving this:
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0, random.uniform(4, 9), self.dfile['foo'])
It seems to be giving the same random value across all values that exceed 0. How do I get different values?
python pandas numpy series
add a comment |
I am probably doing something very simple, but I cant figure out the trick there.
I have a dataframe, and I want to replace the values in a particular column that exceed a value from zero with some random value. I had thought this was a way of achieving this:
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0, random.uniform(4, 9), self.dfile['foo'])
It seems to be giving the same random value across all values that exceed 0. How do I get different values?
python pandas numpy series
add a comment |
I am probably doing something very simple, but I cant figure out the trick there.
I have a dataframe, and I want to replace the values in a particular column that exceed a value from zero with some random value. I had thought this was a way of achieving this:
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0, random.uniform(4, 9), self.dfile['foo'])
It seems to be giving the same random value across all values that exceed 0. How do I get different values?
python pandas numpy series
I am probably doing something very simple, but I cant figure out the trick there.
I have a dataframe, and I want to replace the values in a particular column that exceed a value from zero with some random value. I had thought this was a way of achieving this:
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0, random.uniform(4, 9), self.dfile['foo'])
It seems to be giving the same random value across all values that exceed 0. How do I get different values?
python pandas numpy series
python pandas numpy series
edited Jan 3 at 9:21


jpp
102k2165116
102k2165116
asked Jan 3 at 9:15
tandemtandem
3511323
3511323
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Use numpy.random.uniform
with specifying length by length of DataFrame
:
v = np.random.uniform(4, 9, size=len(self.dfile))
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0, v,self.dfile['foo'])
Sample:
np.random.seed(123)
dfile = pd.DataFrame({
'foo':[0,5.1,1,0,20.4,10.7],
})
v = np.random.uniform(4, 9, size=len(dfile))
dfile['foo1'] = np.where(dfile['foo'] >= 0, v, dfile['foo'])
dfile['foo2'] = np.where(dfile['foo'].between(0, 10), v ,dfile['foo'])
print (dfile)
foo foo1 foo2
0 0.0 7.482346 7.482346
1 5.1 5.430697 5.430697
2 1.0 5.134257 5.134257
3 0.0 6.756574 6.756574
4 20.4 7.597345 20.400000
5 10.7 6.115532 10.700000
Thanks. Would this also function?self.dfile['foo'] = np.where(self.dfile['foo'] >= 0 and self.dfile['foo'] <= 10, v, self.dfile['foo'])
– tandem
Jan 3 at 9:25
2
@tandem - You are close, need&
for bitwise AND and also()
likeself.dfile['foo'] = np.where((self.dfile['foo'] >= 0) & (self.dfile['foo'] <= 10), v, self.dfile['foo'])
– jezrael
Jan 3 at 9:26
@tandem - or betterself.dfile['foo'] = np.where(self.dfile['foo'].between(0, 10), v,self.dfile['foo'])
– jezrael
Jan 3 at 9:27
add a comment |
random.uniform(4, 9)
returns an integer, which np.where
then broadcasts across all rows. Instead, use np.random
, which gives an array of specified length:
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0,
np.random.uniform(4, 9, len(self.dfile.index)),
self.dfile['foo'])
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%2f54019278%2fconditional-replacement-in-pandas-for-each-row%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use numpy.random.uniform
with specifying length by length of DataFrame
:
v = np.random.uniform(4, 9, size=len(self.dfile))
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0, v,self.dfile['foo'])
Sample:
np.random.seed(123)
dfile = pd.DataFrame({
'foo':[0,5.1,1,0,20.4,10.7],
})
v = np.random.uniform(4, 9, size=len(dfile))
dfile['foo1'] = np.where(dfile['foo'] >= 0, v, dfile['foo'])
dfile['foo2'] = np.where(dfile['foo'].between(0, 10), v ,dfile['foo'])
print (dfile)
foo foo1 foo2
0 0.0 7.482346 7.482346
1 5.1 5.430697 5.430697
2 1.0 5.134257 5.134257
3 0.0 6.756574 6.756574
4 20.4 7.597345 20.400000
5 10.7 6.115532 10.700000
Thanks. Would this also function?self.dfile['foo'] = np.where(self.dfile['foo'] >= 0 and self.dfile['foo'] <= 10, v, self.dfile['foo'])
– tandem
Jan 3 at 9:25
2
@tandem - You are close, need&
for bitwise AND and also()
likeself.dfile['foo'] = np.where((self.dfile['foo'] >= 0) & (self.dfile['foo'] <= 10), v, self.dfile['foo'])
– jezrael
Jan 3 at 9:26
@tandem - or betterself.dfile['foo'] = np.where(self.dfile['foo'].between(0, 10), v,self.dfile['foo'])
– jezrael
Jan 3 at 9:27
add a comment |
Use numpy.random.uniform
with specifying length by length of DataFrame
:
v = np.random.uniform(4, 9, size=len(self.dfile))
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0, v,self.dfile['foo'])
Sample:
np.random.seed(123)
dfile = pd.DataFrame({
'foo':[0,5.1,1,0,20.4,10.7],
})
v = np.random.uniform(4, 9, size=len(dfile))
dfile['foo1'] = np.where(dfile['foo'] >= 0, v, dfile['foo'])
dfile['foo2'] = np.where(dfile['foo'].between(0, 10), v ,dfile['foo'])
print (dfile)
foo foo1 foo2
0 0.0 7.482346 7.482346
1 5.1 5.430697 5.430697
2 1.0 5.134257 5.134257
3 0.0 6.756574 6.756574
4 20.4 7.597345 20.400000
5 10.7 6.115532 10.700000
Thanks. Would this also function?self.dfile['foo'] = np.where(self.dfile['foo'] >= 0 and self.dfile['foo'] <= 10, v, self.dfile['foo'])
– tandem
Jan 3 at 9:25
2
@tandem - You are close, need&
for bitwise AND and also()
likeself.dfile['foo'] = np.where((self.dfile['foo'] >= 0) & (self.dfile['foo'] <= 10), v, self.dfile['foo'])
– jezrael
Jan 3 at 9:26
@tandem - or betterself.dfile['foo'] = np.where(self.dfile['foo'].between(0, 10), v,self.dfile['foo'])
– jezrael
Jan 3 at 9:27
add a comment |
Use numpy.random.uniform
with specifying length by length of DataFrame
:
v = np.random.uniform(4, 9, size=len(self.dfile))
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0, v,self.dfile['foo'])
Sample:
np.random.seed(123)
dfile = pd.DataFrame({
'foo':[0,5.1,1,0,20.4,10.7],
})
v = np.random.uniform(4, 9, size=len(dfile))
dfile['foo1'] = np.where(dfile['foo'] >= 0, v, dfile['foo'])
dfile['foo2'] = np.where(dfile['foo'].between(0, 10), v ,dfile['foo'])
print (dfile)
foo foo1 foo2
0 0.0 7.482346 7.482346
1 5.1 5.430697 5.430697
2 1.0 5.134257 5.134257
3 0.0 6.756574 6.756574
4 20.4 7.597345 20.400000
5 10.7 6.115532 10.700000
Use numpy.random.uniform
with specifying length by length of DataFrame
:
v = np.random.uniform(4, 9, size=len(self.dfile))
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0, v,self.dfile['foo'])
Sample:
np.random.seed(123)
dfile = pd.DataFrame({
'foo':[0,5.1,1,0,20.4,10.7],
})
v = np.random.uniform(4, 9, size=len(dfile))
dfile['foo1'] = np.where(dfile['foo'] >= 0, v, dfile['foo'])
dfile['foo2'] = np.where(dfile['foo'].between(0, 10), v ,dfile['foo'])
print (dfile)
foo foo1 foo2
0 0.0 7.482346 7.482346
1 5.1 5.430697 5.430697
2 1.0 5.134257 5.134257
3 0.0 6.756574 6.756574
4 20.4 7.597345 20.400000
5 10.7 6.115532 10.700000
edited Jan 3 at 9:31
answered Jan 3 at 9:19


jezraeljezrael
351k26314389
351k26314389
Thanks. Would this also function?self.dfile['foo'] = np.where(self.dfile['foo'] >= 0 and self.dfile['foo'] <= 10, v, self.dfile['foo'])
– tandem
Jan 3 at 9:25
2
@tandem - You are close, need&
for bitwise AND and also()
likeself.dfile['foo'] = np.where((self.dfile['foo'] >= 0) & (self.dfile['foo'] <= 10), v, self.dfile['foo'])
– jezrael
Jan 3 at 9:26
@tandem - or betterself.dfile['foo'] = np.where(self.dfile['foo'].between(0, 10), v,self.dfile['foo'])
– jezrael
Jan 3 at 9:27
add a comment |
Thanks. Would this also function?self.dfile['foo'] = np.where(self.dfile['foo'] >= 0 and self.dfile['foo'] <= 10, v, self.dfile['foo'])
– tandem
Jan 3 at 9:25
2
@tandem - You are close, need&
for bitwise AND and also()
likeself.dfile['foo'] = np.where((self.dfile['foo'] >= 0) & (self.dfile['foo'] <= 10), v, self.dfile['foo'])
– jezrael
Jan 3 at 9:26
@tandem - or betterself.dfile['foo'] = np.where(self.dfile['foo'].between(0, 10), v,self.dfile['foo'])
– jezrael
Jan 3 at 9:27
Thanks. Would this also function?
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0 and self.dfile['foo'] <= 10, v, self.dfile['foo'])
– tandem
Jan 3 at 9:25
Thanks. Would this also function?
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0 and self.dfile['foo'] <= 10, v, self.dfile['foo'])
– tandem
Jan 3 at 9:25
2
2
@tandem - You are close, need
&
for bitwise AND and also ()
like self.dfile['foo'] = np.where((self.dfile['foo'] >= 0) & (self.dfile['foo'] <= 10), v, self.dfile['foo'])
– jezrael
Jan 3 at 9:26
@tandem - You are close, need
&
for bitwise AND and also ()
like self.dfile['foo'] = np.where((self.dfile['foo'] >= 0) & (self.dfile['foo'] <= 10), v, self.dfile['foo'])
– jezrael
Jan 3 at 9:26
@tandem - or better
self.dfile['foo'] = np.where(self.dfile['foo'].between(0, 10), v,self.dfile['foo'])
– jezrael
Jan 3 at 9:27
@tandem - or better
self.dfile['foo'] = np.where(self.dfile['foo'].between(0, 10), v,self.dfile['foo'])
– jezrael
Jan 3 at 9:27
add a comment |
random.uniform(4, 9)
returns an integer, which np.where
then broadcasts across all rows. Instead, use np.random
, which gives an array of specified length:
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0,
np.random.uniform(4, 9, len(self.dfile.index)),
self.dfile['foo'])
add a comment |
random.uniform(4, 9)
returns an integer, which np.where
then broadcasts across all rows. Instead, use np.random
, which gives an array of specified length:
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0,
np.random.uniform(4, 9, len(self.dfile.index)),
self.dfile['foo'])
add a comment |
random.uniform(4, 9)
returns an integer, which np.where
then broadcasts across all rows. Instead, use np.random
, which gives an array of specified length:
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0,
np.random.uniform(4, 9, len(self.dfile.index)),
self.dfile['foo'])
random.uniform(4, 9)
returns an integer, which np.where
then broadcasts across all rows. Instead, use np.random
, which gives an array of specified length:
self.dfile['foo'] = np.where(self.dfile['foo'] >= 0,
np.random.uniform(4, 9, len(self.dfile.index)),
self.dfile['foo'])
answered Jan 3 at 9:19


jppjpp
102k2165116
102k2165116
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%2f54019278%2fconditional-replacement-in-pandas-for-each-row%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
kOSKg45a1KG z,MmiaqfbQPtD6Q7VBqaaW4FC1AlQEOYPN urTH,dig4WmUurhNTcXhqJKSb k,31xaC5y,HQ7tUUYT4y4IaJ0e