scikit-learn StandardScaler() returns partly NaNs










1















I got stuck with an issue when I apply preprocessing.StandardScaler() to a Pandas DataFrame.



df['total_price'].describe()


returns



count 24895.000000
mean 216.377369
std 161.246931
min 0.000000
25% 109.900000
50% 174.000000
75% 273.000000
max 1355.900000
Name: total_price, dtype: float64


Strangely, after I run



x = df[['total_price']]
standard_scaler = preprocessing.StandardScaler()
x_scaled = standard_scaler.fit_transform(x)
df['new_col'] = pd.DataFrame(x_scaled)


my new column with the standardized values contains also NaNs:



df[['total_price', 'new_col']].head()

total_price new_col
0 241.95 0.158596
1 241.95 0.158596
2 241.95 0.158596
3 81.95 -0.833691
4 81.95 -0.833691

df[['total_price', 'new_col']].tail()

total_price new_col
28167 264.0 NaN
28168 264.0 NaN
28176 94.0 NaN
28177 166.0 NaN
28178 166.0 NaN


What's going wrong here?










share|improve this question

















  • 1





    Your original column had 24895 entries, and your new DF has indices going all the way to 28178, so my first guess that some sort of join or concatenation may have resulted in an index mismatch between the old and new DFs. Were there any intermediate steps not shown, like a train-test split?

    – G. Anderson
    Nov 14 '18 at 19:15











  • it's part of a larger df and I removed rows before. But this was not inbetween the steps above

    – zinyosrim
    Nov 14 '18 at 19:39











  • After reading your comment I did a df = df.reset_index() and the problem got resolved

    – zinyosrim
    Nov 14 '18 at 19:49











  • Glad I could help

    – G. Anderson
    Nov 14 '18 at 20:01















1















I got stuck with an issue when I apply preprocessing.StandardScaler() to a Pandas DataFrame.



df['total_price'].describe()


returns



count 24895.000000
mean 216.377369
std 161.246931
min 0.000000
25% 109.900000
50% 174.000000
75% 273.000000
max 1355.900000
Name: total_price, dtype: float64


Strangely, after I run



x = df[['total_price']]
standard_scaler = preprocessing.StandardScaler()
x_scaled = standard_scaler.fit_transform(x)
df['new_col'] = pd.DataFrame(x_scaled)


my new column with the standardized values contains also NaNs:



df[['total_price', 'new_col']].head()

total_price new_col
0 241.95 0.158596
1 241.95 0.158596
2 241.95 0.158596
3 81.95 -0.833691
4 81.95 -0.833691

df[['total_price', 'new_col']].tail()

total_price new_col
28167 264.0 NaN
28168 264.0 NaN
28176 94.0 NaN
28177 166.0 NaN
28178 166.0 NaN


What's going wrong here?










share|improve this question

















  • 1





    Your original column had 24895 entries, and your new DF has indices going all the way to 28178, so my first guess that some sort of join or concatenation may have resulted in an index mismatch between the old and new DFs. Were there any intermediate steps not shown, like a train-test split?

    – G. Anderson
    Nov 14 '18 at 19:15











  • it's part of a larger df and I removed rows before. But this was not inbetween the steps above

    – zinyosrim
    Nov 14 '18 at 19:39











  • After reading your comment I did a df = df.reset_index() and the problem got resolved

    – zinyosrim
    Nov 14 '18 at 19:49











  • Glad I could help

    – G. Anderson
    Nov 14 '18 at 20:01













1












1








1








I got stuck with an issue when I apply preprocessing.StandardScaler() to a Pandas DataFrame.



df['total_price'].describe()


returns



count 24895.000000
mean 216.377369
std 161.246931
min 0.000000
25% 109.900000
50% 174.000000
75% 273.000000
max 1355.900000
Name: total_price, dtype: float64


Strangely, after I run



x = df[['total_price']]
standard_scaler = preprocessing.StandardScaler()
x_scaled = standard_scaler.fit_transform(x)
df['new_col'] = pd.DataFrame(x_scaled)


my new column with the standardized values contains also NaNs:



df[['total_price', 'new_col']].head()

total_price new_col
0 241.95 0.158596
1 241.95 0.158596
2 241.95 0.158596
3 81.95 -0.833691
4 81.95 -0.833691

df[['total_price', 'new_col']].tail()

total_price new_col
28167 264.0 NaN
28168 264.0 NaN
28176 94.0 NaN
28177 166.0 NaN
28178 166.0 NaN


What's going wrong here?










share|improve this question














I got stuck with an issue when I apply preprocessing.StandardScaler() to a Pandas DataFrame.



df['total_price'].describe()


returns



count 24895.000000
mean 216.377369
std 161.246931
min 0.000000
25% 109.900000
50% 174.000000
75% 273.000000
max 1355.900000
Name: total_price, dtype: float64


Strangely, after I run



x = df[['total_price']]
standard_scaler = preprocessing.StandardScaler()
x_scaled = standard_scaler.fit_transform(x)
df['new_col'] = pd.DataFrame(x_scaled)


my new column with the standardized values contains also NaNs:



df[['total_price', 'new_col']].head()

total_price new_col
0 241.95 0.158596
1 241.95 0.158596
2 241.95 0.158596
3 81.95 -0.833691
4 81.95 -0.833691

df[['total_price', 'new_col']].tail()

total_price new_col
28167 264.0 NaN
28168 264.0 NaN
28176 94.0 NaN
28177 166.0 NaN
28178 166.0 NaN


What's going wrong here?







python pandas scikit-learn






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 18:44









zinyosrimzinyosrim

438414




438414







  • 1





    Your original column had 24895 entries, and your new DF has indices going all the way to 28178, so my first guess that some sort of join or concatenation may have resulted in an index mismatch between the old and new DFs. Were there any intermediate steps not shown, like a train-test split?

    – G. Anderson
    Nov 14 '18 at 19:15











  • it's part of a larger df and I removed rows before. But this was not inbetween the steps above

    – zinyosrim
    Nov 14 '18 at 19:39











  • After reading your comment I did a df = df.reset_index() and the problem got resolved

    – zinyosrim
    Nov 14 '18 at 19:49











  • Glad I could help

    – G. Anderson
    Nov 14 '18 at 20:01












  • 1





    Your original column had 24895 entries, and your new DF has indices going all the way to 28178, so my first guess that some sort of join or concatenation may have resulted in an index mismatch between the old and new DFs. Were there any intermediate steps not shown, like a train-test split?

    – G. Anderson
    Nov 14 '18 at 19:15











  • it's part of a larger df and I removed rows before. But this was not inbetween the steps above

    – zinyosrim
    Nov 14 '18 at 19:39











  • After reading your comment I did a df = df.reset_index() and the problem got resolved

    – zinyosrim
    Nov 14 '18 at 19:49











  • Glad I could help

    – G. Anderson
    Nov 14 '18 at 20:01







1




1





Your original column had 24895 entries, and your new DF has indices going all the way to 28178, so my first guess that some sort of join or concatenation may have resulted in an index mismatch between the old and new DFs. Were there any intermediate steps not shown, like a train-test split?

– G. Anderson
Nov 14 '18 at 19:15





Your original column had 24895 entries, and your new DF has indices going all the way to 28178, so my first guess that some sort of join or concatenation may have resulted in an index mismatch between the old and new DFs. Were there any intermediate steps not shown, like a train-test split?

– G. Anderson
Nov 14 '18 at 19:15













it's part of a larger df and I removed rows before. But this was not inbetween the steps above

– zinyosrim
Nov 14 '18 at 19:39





it's part of a larger df and I removed rows before. But this was not inbetween the steps above

– zinyosrim
Nov 14 '18 at 19:39













After reading your comment I did a df = df.reset_index() and the problem got resolved

– zinyosrim
Nov 14 '18 at 19:49





After reading your comment I did a df = df.reset_index() and the problem got resolved

– zinyosrim
Nov 14 '18 at 19:49













Glad I could help

– G. Anderson
Nov 14 '18 at 20:01





Glad I could help

– G. Anderson
Nov 14 '18 at 20:01












0






active

oldest

votes











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%2f53306854%2fscikit-learn-standardscaler-returns-partly-nans%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















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%2f53306854%2fscikit-learn-standardscaler-returns-partly-nans%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







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3