Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas how to convert all the string value to float

Tags:

python

pandas

I want to convert all the string value in Pandas DataFrame into float, and I can define a short function to do this, but it's not a Pythonic way to do that. My DataFrame looks like this:

>>> df = pd.DataFrame(np.array([['1', '2', '3'], ['4', '5', '6']]))
>>> df
   0  1  2
0  1  2  3
1  4  5  6
>>> df.dtypes
0    object
1    object
2    object
dtype: object
>>> type(df[0][0])
<type 'str'>

I just wonder whether are there some built-in functions of Pandas DataFrame to convert all the string value to float. If you know the built-in function on the Pandas doc, please post the link.

like image 839
GoingMyWay Avatar asked Sep 26 '15 02:09

GoingMyWay


People also ask

How do you convert a string to a float?

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.

How do I change the value of an entire column in pandas?

In order to replace a value in Pandas DataFrame, use the replace() method with the column the from and to values.

How do you change a datatype to a float in Python?

To convert the integer to float, use the float() function in Python. Similarly, if you want to convert a float to an integer, you can use the int() function.


1 Answers

Assuming all values can be correctly converted to float, you can use DataFrame.astype() function to convert the type of complete dataframe to float. Example -

df = df.astype(float)

Demo -

In [5]: df = pd.DataFrame(np.array([['1', '2', '3'], ['4', '5', '6']]))

In [6]: df.astype(float)
Out[6]:
   0  1  2
0  1  2  3
1  4  5  6

In [7]: df = df.astype(float)

In [8]: df.dtypes
Out[8]:
0    float64
1    float64
2    float64
dtype: object

.astype() function also has a raise_on_error argument (which defaults to True) which you can set to False to make it ignore errors . In such cases, the original value is used in the DataFrame -

In [10]: df = pd.DataFrame([['1', '2', '3'], ['4', '5', '6'],['blah','bloh','bleh']])

In [11]: df.astype(float,raise_on_error=False)
Out[11]:
      0     1     2
0     1     2     3
1     4     5     6
2  blah  bloh  bleh

To convert just a series/column to float, again assuming all values can be converted, you can use [Series.astype()][2] . Example -

df['somecol'] = df['somecol'].astype(<type>)
like image 121
Anand S Kumar Avatar answered Oct 08 '22 00:10

Anand S Kumar