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 float
s:
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:None | Name | LONGITUDE | LATITUDE |
---|---|---|---|
0 | alta floresta d'oeste | -61.999824 | -63.033269 |
1 | ariquemes | -1.193554 | -9.908463 |
ValueError: could not convert string to float: 'Null' (Pandas)
Instead of using:
test_dataframe.at[2, 'QTY'] = "Null"
You can use this instead to change the value of any row within any column to "Null"(i.e. In this case I have implemented it to the third row of the third column.test_dataframe.loc[2, "QTY"] = 'Null'
This code worked well in my case, please do comment if you are facing with any issue:
Related Topics
Why Do We Need to Call Zero_Grad() in Pytorch
Finding the Values of the Arrow Keys in Python: Why Are They Triples
Difference Between Variable and Get_Variable in Tensorflow
Numpy: Find Index of the Elements Within Range
Why How to Not Create a Wheel in Python
Is There a Function to Determine Which Quarter of the Year a Date Is In
Disable or Lock Mouse and Keyboard in Python
Scale Matplotlib.Pyplot.Axes.Scatter Markersize by X-Scale
Python Sorting by Multiple Criteria
Logisticregression: Unknown Label Type: 'Continuous' Using Sklearn in Python
Rotating a Two-Dimensional Array in Python
Using Only the Db Part of Django
Basic Program to Convert Integer to Roman Numerals
Pairwise Crossproduct in Python
How to Pickle a Dynamically Created Nested Class in Python
Selenium Webdriver in Python - Files Download Directory Change in Chrome Preferences