1

I am trying to create a heat map but the plot just returns an empty 2D object with no plot. I suspect its because my values are in percentages. is there a function for me to convert all the values to floats? Thank you.

a screenshot of the dataset

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
mlboy
  • 41
  • 6
  • 3
    Does this answer your question? [Convert percent string to float in pandas read\_csv](https://stackoverflow.com/questions/25669588/convert-percent-string-to-float-in-pandas-read-csv) – Mohammad Aug 03 '21 at 15:46
  • 1
    yeah it does, thank you – mlboy Aug 03 '21 at 19:19

1 Answers1

1

Supposing the pd.DataFrame was already created, one approach is to use pandas replace. Say I have the following DataFrame:

df = pd.DataFrame([['10.1%','20.25%'],['40.45%', '50.52%'], ['30.3%', '60.7%']], columns=['foo', 'bar'])

enter image description here

You can use replace to get rid of the % sign:

df = df.replace('%','', regex=True)

enter image description here

Finally, you could cast all columns to np.float64 as needed:

df.astype(np.float64)
luizbarcelos
  • 686
  • 5
  • 17