I got around a SettingWithCopyWarning, feels like the wrong way and computationally inefficient, is there a...
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I encountered the ever-common SettingWithCopyWarning when trying to change some values in a DataFrame. I found a way to get around this without having to disable the warning, but I feel like I've done it the wrong way, and that it is needlessly wasteful and computationally inefficient.
label_encoded_feature_data_to_be_standardised_X_train = X_train_label_encoded[['price', 'vintage']]
label_encoded_feature_data_to_be_standardised_X_test = X_test_label_encoded[['price', 'vintage']]
label_encoded_standard_scaler = StandardScaler()
label_encoded_standard_scaler.fit(label_encoded_feature_data_to_be_standardised_X_train)
X_train_label_encoded_standardised = label_encoded_standard_scaler.transform(label_encoded_feature_data_to_be_standardised_X_train)
X_test_label_encoded_standardised = label_encoded_standard_scaler.transform(label_encoded_feature_data_to_be_standardised_X_test)
That's how it's set up, then I get the warning if I do this:
X_train_label_encoded.loc[:,'price'] = X_train_label_encoded_standardised[:,0]
of if I do this:
X_train_label_encoded_standardised_df = pd.DataFrame(data=X_train_label_encoded_standardised, columns=['price', 'vintage'])
And I solved it by doing this:
X_train_label_encoded = X_train_label_encoded.drop('price', axis=1)
X_train_label_encoded['price'] = X_train_label_encoded_standardised_df.loc[:,'price']
This also works:
X_train_label_encoded.replace(to_replace=X_train_label_encoded['price'], value=X_train_label_encoded_standardised_df['price'])
But even that feels overly clunky with the extra DataFrame creation.
Why can't I just assign the column in some way? Or using some arrangement of the replace method? The documentation doesn't seem to have a solution, or am I just reading it wrong? Missing some obvious but not spelled out solution?
Is there a better way of doing this?
python pandas chained-assignment
add a comment |
I encountered the ever-common SettingWithCopyWarning when trying to change some values in a DataFrame. I found a way to get around this without having to disable the warning, but I feel like I've done it the wrong way, and that it is needlessly wasteful and computationally inefficient.
label_encoded_feature_data_to_be_standardised_X_train = X_train_label_encoded[['price', 'vintage']]
label_encoded_feature_data_to_be_standardised_X_test = X_test_label_encoded[['price', 'vintage']]
label_encoded_standard_scaler = StandardScaler()
label_encoded_standard_scaler.fit(label_encoded_feature_data_to_be_standardised_X_train)
X_train_label_encoded_standardised = label_encoded_standard_scaler.transform(label_encoded_feature_data_to_be_standardised_X_train)
X_test_label_encoded_standardised = label_encoded_standard_scaler.transform(label_encoded_feature_data_to_be_standardised_X_test)
That's how it's set up, then I get the warning if I do this:
X_train_label_encoded.loc[:,'price'] = X_train_label_encoded_standardised[:,0]
of if I do this:
X_train_label_encoded_standardised_df = pd.DataFrame(data=X_train_label_encoded_standardised, columns=['price', 'vintage'])
And I solved it by doing this:
X_train_label_encoded = X_train_label_encoded.drop('price', axis=1)
X_train_label_encoded['price'] = X_train_label_encoded_standardised_df.loc[:,'price']
This also works:
X_train_label_encoded.replace(to_replace=X_train_label_encoded['price'], value=X_train_label_encoded_standardised_df['price'])
But even that feels overly clunky with the extra DataFrame creation.
Why can't I just assign the column in some way? Or using some arrangement of the replace method? The documentation doesn't seem to have a solution, or am I just reading it wrong? Missing some obvious but not spelled out solution?
Is there a better way of doing this?
python pandas chained-assignment
add a comment |
I encountered the ever-common SettingWithCopyWarning when trying to change some values in a DataFrame. I found a way to get around this without having to disable the warning, but I feel like I've done it the wrong way, and that it is needlessly wasteful and computationally inefficient.
label_encoded_feature_data_to_be_standardised_X_train = X_train_label_encoded[['price', 'vintage']]
label_encoded_feature_data_to_be_standardised_X_test = X_test_label_encoded[['price', 'vintage']]
label_encoded_standard_scaler = StandardScaler()
label_encoded_standard_scaler.fit(label_encoded_feature_data_to_be_standardised_X_train)
X_train_label_encoded_standardised = label_encoded_standard_scaler.transform(label_encoded_feature_data_to_be_standardised_X_train)
X_test_label_encoded_standardised = label_encoded_standard_scaler.transform(label_encoded_feature_data_to_be_standardised_X_test)
That's how it's set up, then I get the warning if I do this:
X_train_label_encoded.loc[:,'price'] = X_train_label_encoded_standardised[:,0]
of if I do this:
X_train_label_encoded_standardised_df = pd.DataFrame(data=X_train_label_encoded_standardised, columns=['price', 'vintage'])
And I solved it by doing this:
X_train_label_encoded = X_train_label_encoded.drop('price', axis=1)
X_train_label_encoded['price'] = X_train_label_encoded_standardised_df.loc[:,'price']
This also works:
X_train_label_encoded.replace(to_replace=X_train_label_encoded['price'], value=X_train_label_encoded_standardised_df['price'])
But even that feels overly clunky with the extra DataFrame creation.
Why can't I just assign the column in some way? Or using some arrangement of the replace method? The documentation doesn't seem to have a solution, or am I just reading it wrong? Missing some obvious but not spelled out solution?
Is there a better way of doing this?
python pandas chained-assignment
I encountered the ever-common SettingWithCopyWarning when trying to change some values in a DataFrame. I found a way to get around this without having to disable the warning, but I feel like I've done it the wrong way, and that it is needlessly wasteful and computationally inefficient.
label_encoded_feature_data_to_be_standardised_X_train = X_train_label_encoded[['price', 'vintage']]
label_encoded_feature_data_to_be_standardised_X_test = X_test_label_encoded[['price', 'vintage']]
label_encoded_standard_scaler = StandardScaler()
label_encoded_standard_scaler.fit(label_encoded_feature_data_to_be_standardised_X_train)
X_train_label_encoded_standardised = label_encoded_standard_scaler.transform(label_encoded_feature_data_to_be_standardised_X_train)
X_test_label_encoded_standardised = label_encoded_standard_scaler.transform(label_encoded_feature_data_to_be_standardised_X_test)
That's how it's set up, then I get the warning if I do this:
X_train_label_encoded.loc[:,'price'] = X_train_label_encoded_standardised[:,0]
of if I do this:
X_train_label_encoded_standardised_df = pd.DataFrame(data=X_train_label_encoded_standardised, columns=['price', 'vintage'])
And I solved it by doing this:
X_train_label_encoded = X_train_label_encoded.drop('price', axis=1)
X_train_label_encoded['price'] = X_train_label_encoded_standardised_df.loc[:,'price']
This also works:
X_train_label_encoded.replace(to_replace=X_train_label_encoded['price'], value=X_train_label_encoded_standardised_df['price'])
But even that feels overly clunky with the extra DataFrame creation.
Why can't I just assign the column in some way? Or using some arrangement of the replace method? The documentation doesn't seem to have a solution, or am I just reading it wrong? Missing some obvious but not spelled out solution?
Is there a better way of doing this?
python pandas chained-assignment
python pandas chained-assignment
asked Nov 23 '18 at 15:27
Chor Hatara Hud'u KeturiChor Hatara Hud'u Keturi
3717
3717
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Many times, this warning is just a warning. If your code works and you aren't using chained assignment, you often have nothing to worry about.
If your transformation maintains the index, including order, and your data is numeric, you can use pd.DataFrame.values:
X_train_label_encoded['price'] = X_train_label_encoded_standardised.values[:, 0]
This should sidestep the warning since X_train_label_encoded_standardised.values evaluates to a lower-level NumPy array.
Thank you. In the end I just kept what I had, as it is the most explicit and I don't strictly require that level of efficiency in this case.
– Chor Hatara Hud'u Keturi
Nov 26 '18 at 15:03
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%2f53449321%2fi-got-around-a-settingwithcopywarning-feels-like-the-wrong-way-and-computationa%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
Many times, this warning is just a warning. If your code works and you aren't using chained assignment, you often have nothing to worry about.
If your transformation maintains the index, including order, and your data is numeric, you can use pd.DataFrame.values:
X_train_label_encoded['price'] = X_train_label_encoded_standardised.values[:, 0]
This should sidestep the warning since X_train_label_encoded_standardised.values evaluates to a lower-level NumPy array.
Thank you. In the end I just kept what I had, as it is the most explicit and I don't strictly require that level of efficiency in this case.
– Chor Hatara Hud'u Keturi
Nov 26 '18 at 15:03
add a comment |
Many times, this warning is just a warning. If your code works and you aren't using chained assignment, you often have nothing to worry about.
If your transformation maintains the index, including order, and your data is numeric, you can use pd.DataFrame.values:
X_train_label_encoded['price'] = X_train_label_encoded_standardised.values[:, 0]
This should sidestep the warning since X_train_label_encoded_standardised.values evaluates to a lower-level NumPy array.
Thank you. In the end I just kept what I had, as it is the most explicit and I don't strictly require that level of efficiency in this case.
– Chor Hatara Hud'u Keturi
Nov 26 '18 at 15:03
add a comment |
Many times, this warning is just a warning. If your code works and you aren't using chained assignment, you often have nothing to worry about.
If your transformation maintains the index, including order, and your data is numeric, you can use pd.DataFrame.values:
X_train_label_encoded['price'] = X_train_label_encoded_standardised.values[:, 0]
This should sidestep the warning since X_train_label_encoded_standardised.values evaluates to a lower-level NumPy array.
Many times, this warning is just a warning. If your code works and you aren't using chained assignment, you often have nothing to worry about.
If your transformation maintains the index, including order, and your data is numeric, you can use pd.DataFrame.values:
X_train_label_encoded['price'] = X_train_label_encoded_standardised.values[:, 0]
This should sidestep the warning since X_train_label_encoded_standardised.values evaluates to a lower-level NumPy array.
edited Nov 23 '18 at 17:09
answered Nov 23 '18 at 17:04
jppjpp
103k2166116
103k2166116
Thank you. In the end I just kept what I had, as it is the most explicit and I don't strictly require that level of efficiency in this case.
– Chor Hatara Hud'u Keturi
Nov 26 '18 at 15:03
add a comment |
Thank you. In the end I just kept what I had, as it is the most explicit and I don't strictly require that level of efficiency in this case.
– Chor Hatara Hud'u Keturi
Nov 26 '18 at 15:03
Thank you. In the end I just kept what I had, as it is the most explicit and I don't strictly require that level of efficiency in this case.
– Chor Hatara Hud'u Keturi
Nov 26 '18 at 15:03
Thank you. In the end I just kept what I had, as it is the most explicit and I don't strictly require that level of efficiency in this case.
– Chor Hatara Hud'u Keturi
Nov 26 '18 at 15:03
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%2f53449321%2fi-got-around-a-settingwithcopywarning-feels-like-the-wrong-way-and-computationa%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