Dataframe Change Column type

Source: Internet
Author: User

An error occurred today in the process of finding the inverse of a matrix using the NumPy Linalg.det ():
Typeerror:no loop matching the specified signature and casting is found for Ufunc
Check a half-day found is the problem of data types,numpy in the inverse of the time will first check the data type is consistent, if inconsistent will be an error (say this wrong message is too difficult to understand, but also look at the source O (╯-╰) o).
Because my data is used pandas. Dataframe read, so the data type of each column may be different.
Back to check the data, sure enough to have some int, some float. So all changed to float64 type.
The following methods were found, as well as the Dataframe data type: dataframe type conversion Method-astype ()

Import pandas as PD
DF = PD. Dataframe ([{' col1 ': ' A ', ' col2 ': ' 1 '}, {' col1 ': ' B ', ' col2 ': ' 2 '}]) print df.dtypes df[' col2 '

] = df[' col2 ']. Astype (' int ')
print '-----------'
print df.dtypes

df[' col2 '] = df[' col2 '].astype (' float64 ')
print ' -----------'
print df.dtypes

Output:

Col1    Object
col2    object
dtype:object
-----------
col1    Object
col2     int32
dtype:object
-----------
col1     object
col2    float64
dtype:object

Astype () can also change the type of all data at once:

In[30]:a
out[31]: 
          a         b         c         d
0  0.891380  0.442167-0.539450  1.023458
1- 0.488131-1.847104-0.209799-0.768713
2  1.290434  0.327096  0.358406  0.422209

in[32] : A.astype (' int32 ')
out[32]: 
   a  b  c  d
0  0  0  0  1  1 0- 1  0  0
2  1  0 0 0

Attached: Data Type list

The Data type Description bool_ Boolean (True or False) stored as a byte int_ Default integer type (same as C long; nor Mally either Int64 or Int32) Intc identical to C int (normally int32 or Int64) intp Integer used for indexing (same As C ssize_t; Normally either Int32 or Int64) int8 Byte ( -128 to 127) Int16 integer ( -32768 to 32767) Int32 integer (-2147483648 to 2147483647) Int64 integer ( -9223372036854775808 to 9223372036854775807) uint8 Unsigned Integer (0 to 255) uint16 U nsigned Integer (0 to 65535) uint32 Unsigned Integer (0 to 4294967295) UInt64 Unsigned integer (0 to 1844674407370955161
5) Float_ shorthand for float64.  Float16 Half Precision Float:sign bit, 5 bits exponent, ten bits mantissa float32 single precision bit, 8 bits Exponent bits mantissa float64 Double precision float:sign bit, one bits exponent, bits mantissa complex_ short
Hand for complex128. complex64 Complex number, represented by two 32-bit floats (real and ImaginARY Components complex128 Complex number, represented by two 64-bit floats (real and imaginary components) 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.