How to convert object format to float64 format losslessly

php中世界最好的语言
Release: 2018-04-12 09:26:54
Original
4790 people have browsed it

This time I will bring you object format lossless conversion to float64 format, object format lossless conversion to float64 format What are the precautions, the following is a practical case, let's come together take a look.

In the process of data processing

For example, importing data from a CSV file

data_df = pd.read_csv("names.csv")
Copy after login

Be sure to check the type of data before processing

data_df.info()
Copy after login
*RangeIndex: 891 entries, 0 to 890 
Data columns (total 12 columns): 
Name 891 non-null object 
Sex 891 non-null object 
Age 714 non-null float64 
SibSp 891 non-null int64 
Parch 891 non-null int64 
Ticket 891 non-null object 
Fare 891 non-null float64 
Cabin 204 non-null object 
Embarked 889 non-null object 
dtypes: float64(2), int64(5), object(5) 
memory usage: 83.6+ KB*
Copy after login

The above object, int64, and float64 are the types of data.

If we need to perform operations on column data, one thing we must pay attention to is:

Whether the data types of the two columns are the same !!

If an object class type is added to an int64 type, an error will occur

The error message may be as follows:

TypeError: ufunc 'add' not contain a loop with signature matching types dtype('<U32') dtype('<U32') dtype('<U32')
Copy after login

The object type at this time may be a number in str format like '12.3'. If you want to perform operations, format conversion must be performed:

can be used as follows Method (convert_objects):

dt_df = dt_df.convert_objects(convert_numeric=True)
Copy after login

Personal test is valid.

Remind me again! Before getting data, be sure to check the data type first! ! !

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to convert object into float data

How to bidirectionally bind Vue2 parent component and child component accomplish

The above is the detailed content of How to convert object format to float64 format losslessly. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!