Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to save the values from populatioEst column in float variables using Python3 & Pandas, I have the following table:

Name populationEst
Amsterdam 872757
Netherlands 17407585
question from:https://stackoverflow.com/questions/65645140/converting-data-frame-entry-to-float-in-python-pandas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
464 views
Welcome To Ask or Share your Answers For Others

1 Answer

Is this what are you looking for?

populationAM = pops.loc[pops.Name == 'Amsterdam', 'populationEst'].iloc[0]
populationNL = pops.loc[pops.Name == 'Netherlands', 'populationEst'].iloc[0]
frac = populationAM * 100 / populationNL

The value of frac here is 5.013659275539944, while populationAM and populationNL are the integers corresponding to the respective populations (as you can see, the type of these variables is not a problem to compute the correct value of frac). In your code, the issue is that populationAM and populationNL are pandas Series, instead of integers; iloc[0] retrieves the value in the first position of the series.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...