Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: could not convert string to float: '"152.7"'

I have a dataframe which I created from a dictionary like so: pd.DataFrame.from_dict(dict1, dtype=str)

however , the datatypes for all fields are showing up as "Object"

I want to convert some of the columns to int and/or float, but I am unable to do it even after trying several ways.

I have tried the following ways :

df['duration'].astype(int) 
df['duration'].astype(str).astype(int) 
df['duration'].replace('"','').astype(int) 

ValueError: invalid literal for int() with base 10: '"467900"'

df['cpu'].astype(float)
df['cpu'].astype(str).astype(float)
df['cpu'].replace('"','').astype(float)

ValueError: could not convert string to float: '"152.7"'

This is my dataframe :

    duration    realtime    cpu
0   "268641"    "46871" "152.7"
1   "208642"    "2709"  "107.1"
2   "208817"    "2163"  "108.2"
3   "238558"    "9307"  "141.1"
4   "208881"    "2729"  "106.7"

Please let me know how I can make this work.

Thanks in advance! Please let me know how I can get this to work.

Thanks in advance!

like image 723
rkm19 Avatar asked Feb 27 '26 13:02

rkm19


2 Answers

df=df.replace(regex='[^\d\.]', value='')#Remove any non digits except the decimal point

#Then now convert as you want
df['realtime']=df['realtime'].astype(int)
df['cpu']=df['cpu'].astype(float)
like image 165
wwnde Avatar answered Mar 02 '26 07:03

wwnde


In Addition to @wwnde answer, you could also perfrom this operation in one line as follows:

df.replace(regex='[^\d\.]', value='').astype({
    'duration' : int,
    'realtime' : int,
    'cpu' : float
})

Output:

   duration  realtime    cpu
0    268641     46871  152.7
1    208642      2709  107.1
2    208817      2163  108.2
3    238558      9307  141.1
4    208881      2729  106.7
like image 37
Antoine Dubuis Avatar answered Mar 02 '26 05:03

Antoine Dubuis