当前位置:网站首页>Use to_ Numeric to numeric type

Use to_ Numeric to numeric type

2022-06-26 04:50:00 I am a little monster

Catalog

 errors Parameters :

 to_numeric Move down :downcast Parameters


to_numeric: On astype The conversion data type method is better at converting non numeric data to numeric data types ,

astype Convert data type _ I am a little monster blog -CSDN Blog

import pandas as pd 
import seaborn as sns

tips=sns.load_dataset('tips')
t=tips.head(10)# Get subsets 
t.loc[[1,4,7],'total_bill']='missing'# Modify three of the data to missing
print(t)
print(t.dtypes)# View data type 

Output is as follows :

  total_bill   tip     sex smoker  day    time  size
0      16.99  1.01  Female     No  Sun  Dinner     2
1    missing  1.66    Male     No  Sun  Dinner     3
2      21.01  3.50    Male     No  Sun  Dinner     3
3      23.68  3.31    Male     No  Sun  Dinner     2
4    missing  3.61  Female     No  Sun  Dinner     4
5      25.29  4.71    Male     No  Sun  Dinner     4
6       8.77  2.00    Male     No  Sun  Dinner     2
7    missing  3.12    Male     No  Sun  Dinner     4
8      15.04  1.96    Male     No  Sun  Dinner     2
9      14.78  3.23    Male     No  Sun  Dinner     2
------------------------------------
total_bill      object
tip            float64
sex           category
smoker        category
day           category
time          category
size             int64
dtype: object
[Finished in 1.8s]

t['total_bill'].astype(float)# At this point, the string type is converted to float The following error occurs for type 
pd.to_numeric(t['total_bill'])

  of astype We can refer to astype Convert data type _ I am a little monster blog -CSDN Blog

ValueError: could not convert string to float: 'missing'

ValueError: Unable to parse string "missing" at position 1

 errors Parameters :

to_numeric Function has an argument errors, Determines how a function should handle a value that cannot be converted to a numeric value , The default value is raise, That is, an error will be caused when a payment request cannot be converted .

errors The parameter has the following three values :

(1)raise: The default value is , An error is reported when the conversion cannot be performed .

(2)coerce: When the conversion is not possible, it returns NaN( Missing value ).

(3)ignore: Give up the conversion when it is impossible to convert , Directly return the entire column ( Don't do anything? ).

import pandas as pd 
import seaborn as sns

tips=sns.load_dataset('tips')
t=tips.head(10)
t.loc[[1,4,7],'total_bill']='missing'

t['total_bill']=pd.to_numeric(t['total_bill'],errors='coerce')#errors Parameter specified as coerce
print(t)

The output is as follows :

   total_bill   tip     sex smoker  day    time  size
0       16.99  1.01  Female     No  Sun  Dinner     2
1         NaN  1.66    Male     No  Sun  Dinner     3
2       21.01  3.50    Male     No  Sun  Dinner     3
3       23.68  3.31    Male     No  Sun  Dinner     2
4         NaN  3.61  Female     No  Sun  Dinner     4
5       25.29  4.71    Male     No  Sun  Dinner     4
6        8.77  2.00    Male     No  Sun  Dinner     2
7         NaN  3.12    Male     No  Sun  Dinner     4
8       15.04  1.96    Male     No  Sun  Dinner     2
9       14.78  3.23    Male     No  Sun  Dinner     2
[Finished in 1.8s]

 to_numeric Move down :downcast Parameters

Allow columns to be converted to numeric types , Change the value type to the smallest value type , The default value is None, Other possible values are integer,signed,unsigned and float

import pandas as pd 
import seaborn as sns

tips=sns.load_dataset('tips')
t=tips.head(10)
t.loc[[1,4,7],'total_bill']='missing'

t['total_bill']=pd.to_numeric(t['total_bill'],errors='coerce')# Don't specify downcast Parameters 
print(t.dtypes)
print('--------'*6)
t['total_bill']=pd.to_numeric(t['total_bill'],errors='coerce',downcast='float')# Appoint downcast Parameters 
print(t.dtypes)

You can see , Appoint downcast After the parameters ,total_bill The data type of is from float64 Turned into float32, The memory used is smaller

The output is as follows :

total_bill     float64
tip            float64
sex           category
smoker        category
day           category
time          category
size             int64
dtype: object
------------------------------------------------
total_bill     float32
tip            float64
sex           category
smoker        category
day           category
time          category
size             int64
dtype: object
[Finished in 1.7s]

原网站

版权声明
本文为[I am a little monster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180510153713.html