Converting Strings to Floats in a Dataframe

Converting string to float - python

The solution that worked in my case was to add an argument errors='coerce' to the to_numeric function.

df["mycolumn"] = pd.to_numeric(df["mycolumn"], errors='coerce', downcast="float")

Converting string to float in pandas

Try replace with regex=True option:

new_df = (new_df[['Eleven', 'Twelve', 'Thirteen', 'Fourteen']]
.replace(',', '', regex=True)
.astype(float)
)

Or use apply:

new_df = (new_df[['Eleven', 'Twelve', 'Thirteen', 'Fourteen']]
.apply(lambda x: x.str.replace(',', ''))
.astype(float)
)

Converting a pandas dataframe column values from string to float

Try this:

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

Convert strings to float in all pandas columns, where this is possible

I think you need parameter errors='ignore' in to_numeric:

df = df.apply(pd.to_numeric, errors='ignore')
print (df.dtypes)
A object
B int64
C float64
dtype: object

It working nice if not mixed values - numeric with strings:

df_list = [["a", "t", "2"], ["b", "3", np.nan]]
df = pd.DataFrame(df_list, columns = list("ABC"))

df = df.apply(pd.to_numeric, errors='ignore')
print (df)
A B C
0 a t 2.0 <=added t to column B for mixed values
1 b 3 NaN

print (df.dtypes)
A object
B object
C float64
dtype: object

EDIT:

You can downcast also int to floats:

df = df.apply(pd.to_numeric, errors='ignore', downcast='float')
print (df.dtypes)
A object
B float32
C float32
dtype: object

It is same as:

df = df.apply(lambda x: pd.to_numeric(x, errors='ignore', downcast='float'))
print (df.dtypes)
A object
B float32
C float32
dtype: object

Transform string that should be list of floats in a column of dataframe?

Use ast.literal_eval:

import ast

df['interval'] = df['interval'].apply(ast.literal_eval)

Output

>>> df
interval
0 [100.0, 3.0]
1 [3.0, 2.0]
2 [2.0, 1.0]
3 [1, 0.25]
4 [0.25, 0.0]

>>> df.loc[0, 'interval']
[100.0, 3.0]

>>> type(df.loc[0, 'interval'])
list

Now you can convert to columns if you want:

>>> df['interval'].apply(pd.Series)
0 1
0 100.00 3.00
1 3.00 2.00
2 2.00 1.00
3 1.00 0.25
4 0.25 0.00

Convert strings with a - (En Dashes) into a floats

How i thought the string should be made to floats.

import pandas as pd

df = pd.DataFrame(
{
"Name": ["alta floresta d'oeste", "ariquemes"],
"LONGITUDE" : ["-61.999.824", "-1.193.554"],
"LATITUDE" : ["-63.033.269", "-9.908.463"]
}
)

def remove_dot(s):
s = s.split(".")
x = s[0] + "." + "".join(s[1:])
return(x)

new_df = pd.concat(
[
df["Name"],
df["LONGITUDE"].apply(remove_dot),
df["LATITUDE"].apply(remove_dot)
],
axis = 1
)

new_df = new_df.astype({'LONGITUDE':'float','LATITUDE':'float'})

new_df.head()

Gives me this:



Leave a reply



Submit