Can't replace string symbol “-” in dataframe on jupyter notebook












0















I import data from "url = ("http://finviz.com/quote.ashx?t=" + symbol.lower())"



and got the table:



        P/B    P/E Forward P/E   PEG Debt/Eq EPS (ttm) Dividend %     ROE  
AMZN 18.73 92.45 56.23 2.09 1.21 16.25 - 26.70%
GOOG 4.24 38.86 - 2.55 - 26.65 - -
PG 4.47 22.67 19.47 3.45 0.61 4.05 3.12% 18.80%
KO 11.04 30.26 21.36 4.50 2.45 1.57 3.29% 15.10%
IBM 5.24 9.28 8.17 9.67 2.37 12.25 5.52% 30.90%

ROI EPS Q/Q Insider Own
AMZN 3.50% 1026.20% 16.20%
GOOG - 36.50% 5.74%
PG 13.10% 15.50% 0.10%
KO 12.50% 56.80% 0.10%
IBM 17.40% 0.70% 0.10%


Then I was trying to convert string to float:



df = df[(df['P/E'].astype(float)<20) & (df['P/B'].astype(float) < 3)] 


and got "ValueError: could not convert string to float:"



I think that values 0.70% and sign "-" is the problem.



I tried:



df.replace("-","0")
df.replace('-', 0)
df.replace('-', nan)


But nothing works.










share|improve this question

























  • Minus sign is not a problem for casting to float. But % is.

    – SpghttCd
    Jan 1 at 23:31
















0















I import data from "url = ("http://finviz.com/quote.ashx?t=" + symbol.lower())"



and got the table:



        P/B    P/E Forward P/E   PEG Debt/Eq EPS (ttm) Dividend %     ROE  
AMZN 18.73 92.45 56.23 2.09 1.21 16.25 - 26.70%
GOOG 4.24 38.86 - 2.55 - 26.65 - -
PG 4.47 22.67 19.47 3.45 0.61 4.05 3.12% 18.80%
KO 11.04 30.26 21.36 4.50 2.45 1.57 3.29% 15.10%
IBM 5.24 9.28 8.17 9.67 2.37 12.25 5.52% 30.90%

ROI EPS Q/Q Insider Own
AMZN 3.50% 1026.20% 16.20%
GOOG - 36.50% 5.74%
PG 13.10% 15.50% 0.10%
KO 12.50% 56.80% 0.10%
IBM 17.40% 0.70% 0.10%


Then I was trying to convert string to float:



df = df[(df['P/E'].astype(float)<20) & (df['P/B'].astype(float) < 3)] 


and got "ValueError: could not convert string to float:"



I think that values 0.70% and sign "-" is the problem.



I tried:



df.replace("-","0")
df.replace('-', 0)
df.replace('-', nan)


But nothing works.










share|improve this question

























  • Minus sign is not a problem for casting to float. But % is.

    – SpghttCd
    Jan 1 at 23:31














0












0








0








I import data from "url = ("http://finviz.com/quote.ashx?t=" + symbol.lower())"



and got the table:



        P/B    P/E Forward P/E   PEG Debt/Eq EPS (ttm) Dividend %     ROE  
AMZN 18.73 92.45 56.23 2.09 1.21 16.25 - 26.70%
GOOG 4.24 38.86 - 2.55 - 26.65 - -
PG 4.47 22.67 19.47 3.45 0.61 4.05 3.12% 18.80%
KO 11.04 30.26 21.36 4.50 2.45 1.57 3.29% 15.10%
IBM 5.24 9.28 8.17 9.67 2.37 12.25 5.52% 30.90%

ROI EPS Q/Q Insider Own
AMZN 3.50% 1026.20% 16.20%
GOOG - 36.50% 5.74%
PG 13.10% 15.50% 0.10%
KO 12.50% 56.80% 0.10%
IBM 17.40% 0.70% 0.10%


Then I was trying to convert string to float:



df = df[(df['P/E'].astype(float)<20) & (df['P/B'].astype(float) < 3)] 


and got "ValueError: could not convert string to float:"



I think that values 0.70% and sign "-" is the problem.



I tried:



df.replace("-","0")
df.replace('-', 0)
df.replace('-', nan)


But nothing works.










share|improve this question
















I import data from "url = ("http://finviz.com/quote.ashx?t=" + symbol.lower())"



and got the table:



        P/B    P/E Forward P/E   PEG Debt/Eq EPS (ttm) Dividend %     ROE  
AMZN 18.73 92.45 56.23 2.09 1.21 16.25 - 26.70%
GOOG 4.24 38.86 - 2.55 - 26.65 - -
PG 4.47 22.67 19.47 3.45 0.61 4.05 3.12% 18.80%
KO 11.04 30.26 21.36 4.50 2.45 1.57 3.29% 15.10%
IBM 5.24 9.28 8.17 9.67 2.37 12.25 5.52% 30.90%

ROI EPS Q/Q Insider Own
AMZN 3.50% 1026.20% 16.20%
GOOG - 36.50% 5.74%
PG 13.10% 15.50% 0.10%
KO 12.50% 56.80% 0.10%
IBM 17.40% 0.70% 0.10%


Then I was trying to convert string to float:



df = df[(df['P/E'].astype(float)<20) & (df['P/B'].astype(float) < 3)] 


and got "ValueError: could not convert string to float:"



I think that values 0.70% and sign "-" is the problem.



I tried:



df.replace("-","0")
df.replace('-', 0)
df.replace('-', nan)


But nothing works.







python pandas replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 0:18









Wen-Ben

114k83368




114k83368










asked Jan 1 at 23:24









basta burburlajevbasta burburlajev

1




1













  • Minus sign is not a problem for casting to float. But % is.

    – SpghttCd
    Jan 1 at 23:31



















  • Minus sign is not a problem for casting to float. But % is.

    – SpghttCd
    Jan 1 at 23:31

















Minus sign is not a problem for casting to float. But % is.

– SpghttCd
Jan 1 at 23:31





Minus sign is not a problem for casting to float. But % is.

– SpghttCd
Jan 1 at 23:31












2 Answers
2






active

oldest

votes


















0














You may need to assign it back



df=df.replace("-","0")


And I recommend to_numeric



df['P/E']=pd.to_numeric(df['P/E'],errors = 'coerce')
df['P/B']=pd.to_numeric(df['P/B'],errors = 'coerce')





share|improve this answer


























  • Thank you it works fine

    – basta burburlajev
    Jan 2 at 11:38



















0














You should use numpy:



import numpy as np


then the next replacement:



df = df.replace('-', np.nan)


Next, change the datatype:



df = df['Forward P/E'].astype(float)


Lastly, you can test if the datatype is float64.






share|improve this answer
























  • Thank you Andres, it works.

    – basta burburlajev
    Jan 3 at 23:07











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%2f53999724%2fcant-replace-string-symbol-in-dataframe-on-jupyter-notebook%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









0














You may need to assign it back



df=df.replace("-","0")


And I recommend to_numeric



df['P/E']=pd.to_numeric(df['P/E'],errors = 'coerce')
df['P/B']=pd.to_numeric(df['P/B'],errors = 'coerce')





share|improve this answer


























  • Thank you it works fine

    – basta burburlajev
    Jan 2 at 11:38
















0














You may need to assign it back



df=df.replace("-","0")


And I recommend to_numeric



df['P/E']=pd.to_numeric(df['P/E'],errors = 'coerce')
df['P/B']=pd.to_numeric(df['P/B'],errors = 'coerce')





share|improve this answer


























  • Thank you it works fine

    – basta burburlajev
    Jan 2 at 11:38














0












0








0







You may need to assign it back



df=df.replace("-","0")


And I recommend to_numeric



df['P/E']=pd.to_numeric(df['P/E'],errors = 'coerce')
df['P/B']=pd.to_numeric(df['P/B'],errors = 'coerce')





share|improve this answer















You may need to assign it back



df=df.replace("-","0")


And I recommend to_numeric



df['P/E']=pd.to_numeric(df['P/E'],errors = 'coerce')
df['P/B']=pd.to_numeric(df['P/B'],errors = 'coerce')






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 0:28

























answered Jan 2 at 0:19









Wen-BenWen-Ben

114k83368




114k83368













  • Thank you it works fine

    – basta burburlajev
    Jan 2 at 11:38



















  • Thank you it works fine

    – basta burburlajev
    Jan 2 at 11:38

















Thank you it works fine

– basta burburlajev
Jan 2 at 11:38





Thank you it works fine

– basta burburlajev
Jan 2 at 11:38













0














You should use numpy:



import numpy as np


then the next replacement:



df = df.replace('-', np.nan)


Next, change the datatype:



df = df['Forward P/E'].astype(float)


Lastly, you can test if the datatype is float64.






share|improve this answer
























  • Thank you Andres, it works.

    – basta burburlajev
    Jan 3 at 23:07
















0














You should use numpy:



import numpy as np


then the next replacement:



df = df.replace('-', np.nan)


Next, change the datatype:



df = df['Forward P/E'].astype(float)


Lastly, you can test if the datatype is float64.






share|improve this answer
























  • Thank you Andres, it works.

    – basta burburlajev
    Jan 3 at 23:07














0












0








0







You should use numpy:



import numpy as np


then the next replacement:



df = df.replace('-', np.nan)


Next, change the datatype:



df = df['Forward P/E'].astype(float)


Lastly, you can test if the datatype is float64.






share|improve this answer













You should use numpy:



import numpy as np


then the next replacement:



df = df.replace('-', np.nan)


Next, change the datatype:



df = df['Forward P/E'].astype(float)


Lastly, you can test if the datatype is float64.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 15:05









Antonio AndrésAntonio Andrés

1039




1039













  • Thank you Andres, it works.

    – basta burburlajev
    Jan 3 at 23:07



















  • Thank you Andres, it works.

    – basta burburlajev
    Jan 3 at 23:07

















Thank you Andres, it works.

– basta burburlajev
Jan 3 at 23:07





Thank you Andres, it works.

– basta burburlajev
Jan 3 at 23:07


















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%2f53999724%2fcant-replace-string-symbol-in-dataframe-on-jupyter-notebook%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







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas