Python Pandas: Conditional Updating of Column Values Based on Another Col ('int' object is not subscriptable)...

Multi tool use
I have the following sample data frame. col_2 is an integer datatype
col_1 col_2
(hello, you, good) 65
(yes, are, tiles) 23
I want my code to replace the value in col_2 if col_1 contains a certain string. Otherwise, leave col_2 value as is.
Below is my code:
df['col_3'] = np.where(df['col_1'] == '(hello, you, good)', 18, df['col_2'])
Below is my desired output:
col_1 col_2
(hello, you, good) 18
(yes, are, tiles) 23
But when I run my code above, I get the following error:
TypeError: 'int' object is not subscriptable
python pandas conditional where
add a comment |
I have the following sample data frame. col_2 is an integer datatype
col_1 col_2
(hello, you, good) 65
(yes, are, tiles) 23
I want my code to replace the value in col_2 if col_1 contains a certain string. Otherwise, leave col_2 value as is.
Below is my code:
df['col_3'] = np.where(df['col_1'] == '(hello, you, good)', 18, df['col_2'])
Below is my desired output:
col_1 col_2
(hello, you, good) 18
(yes, are, tiles) 23
But when I run my code above, I get the following error:
TypeError: 'int' object is not subscriptable
python pandas conditional where
Your code works for this sample (except it should bedf['col_2']
). Can you make it such that we can reproduce your problem?
– coldspeed
Jan 2 at 15:57
you are right; I made the col name edit
– PineNuts0
Jan 2 at 16:01
1
Your code still works for this example.
– coldspeed
Jan 2 at 16:03
add a comment |
I have the following sample data frame. col_2 is an integer datatype
col_1 col_2
(hello, you, good) 65
(yes, are, tiles) 23
I want my code to replace the value in col_2 if col_1 contains a certain string. Otherwise, leave col_2 value as is.
Below is my code:
df['col_3'] = np.where(df['col_1'] == '(hello, you, good)', 18, df['col_2'])
Below is my desired output:
col_1 col_2
(hello, you, good) 18
(yes, are, tiles) 23
But when I run my code above, I get the following error:
TypeError: 'int' object is not subscriptable
python pandas conditional where
I have the following sample data frame. col_2 is an integer datatype
col_1 col_2
(hello, you, good) 65
(yes, are, tiles) 23
I want my code to replace the value in col_2 if col_1 contains a certain string. Otherwise, leave col_2 value as is.
Below is my code:
df['col_3'] = np.where(df['col_1'] == '(hello, you, good)', 18, df['col_2'])
Below is my desired output:
col_1 col_2
(hello, you, good) 18
(yes, are, tiles) 23
But when I run my code above, I get the following error:
TypeError: 'int' object is not subscriptable
python pandas conditional where
python pandas conditional where
edited Jan 2 at 16:01
PineNuts0
asked Jan 2 at 15:56
PineNuts0PineNuts0
77121430
77121430
Your code works for this sample (except it should bedf['col_2']
). Can you make it such that we can reproduce your problem?
– coldspeed
Jan 2 at 15:57
you are right; I made the col name edit
– PineNuts0
Jan 2 at 16:01
1
Your code still works for this example.
– coldspeed
Jan 2 at 16:03
add a comment |
Your code works for this sample (except it should bedf['col_2']
). Can you make it such that we can reproduce your problem?
– coldspeed
Jan 2 at 15:57
you are right; I made the col name edit
– PineNuts0
Jan 2 at 16:01
1
Your code still works for this example.
– coldspeed
Jan 2 at 16:03
Your code works for this sample (except it should be
df['col_2']
). Can you make it such that we can reproduce your problem?– coldspeed
Jan 2 at 15:57
Your code works for this sample (except it should be
df['col_2']
). Can you make it such that we can reproduce your problem?– coldspeed
Jan 2 at 15:57
you are right; I made the col name edit
– PineNuts0
Jan 2 at 16:01
you are right; I made the col name edit
– PineNuts0
Jan 2 at 16:01
1
1
Your code still works for this example.
– coldspeed
Jan 2 at 16:03
Your code still works for this example.
– coldspeed
Jan 2 at 16:03
add a comment |
1 Answer
1
active
oldest
votes
I guess that is not string type column for col_1
np.where(df['col_1'] == ('hello', 'you', 'good'), 18, df['col_2'])
Out[759]: array([18, 23], dtype=int64)
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%2f54009344%2fpython-pandas-conditional-updating-of-column-values-based-on-another-col-int%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 guess that is not string type column for col_1
np.where(df['col_1'] == ('hello', 'you', 'good'), 18, df['col_2'])
Out[759]: array([18, 23], dtype=int64)
add a comment |
I guess that is not string type column for col_1
np.where(df['col_1'] == ('hello', 'you', 'good'), 18, df['col_2'])
Out[759]: array([18, 23], dtype=int64)
add a comment |
I guess that is not string type column for col_1
np.where(df['col_1'] == ('hello', 'you', 'good'), 18, df['col_2'])
Out[759]: array([18, 23], dtype=int64)
I guess that is not string type column for col_1
np.where(df['col_1'] == ('hello', 'you', 'good'), 18, df['col_2'])
Out[759]: array([18, 23], dtype=int64)
answered Jan 2 at 16:06


Wen-BenWen-Ben
117k83469
117k83469
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%2f54009344%2fpython-pandas-conditional-updating-of-column-values-based-on-another-col-int%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
676v7 j aQ3nvXG9dw MUKOIGday496jDVNpszLp2g,CMQqJMRGHf 35EU,e7szeLGhsDbkvT6,D95nIeo,76A,YIfL YFI3lXjLWIFjZJQf
Your code works for this sample (except it should be
df['col_2']
). Can you make it such that we can reproduce your problem?– coldspeed
Jan 2 at 15:57
you are right; I made the col name edit
– PineNuts0
Jan 2 at 16:01
1
Your code still works for this example.
– coldspeed
Jan 2 at 16:03