当前位置:网站首页>Use to_ Numeric to numeric type
Use to_ Numeric to numeric type
2022-06-26 04:50:00 【I am a little monster】
Catalog
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]
边栏推荐
猜你喜欢
Multipass Chinese document - setup driver
08_ Spingboot integrated redis
Stm8 MCU ADC sampling function is triggered by timer
ROS 笔记(07)— 客户端 Client 和服务端 Server 的实现
1.18 learning summary
【Latex】错误类型总结(持更)
Database design (I)
Statsmodels Library -- linear regression model
YOLOV5超参数设置与数据增强解析
Dameng database backup and restore
随机推荐
2022.1.24
BACK-OFF RESTARTING FAILED CONTAINER 的解决方法
Yolov5 super parameter setting and data enhancement analysis
企业的产品服务怎么进行口碑营销?口碑营销可以找人代做吗?
0622-马棕榈跌9%
ROS 笔记(07)— 客户端 Client 和服务端 Server 的实现
Simple application of KMP
1.17 learning summary
dijkstra
Text horizontal alignment attribute text align and element vertical alignment attribute vertical align
JWT token authentication verification
Zhimeng CMS will file a lawsuit against infringing websites
Using Matplotlib to add an external image at the canvas level
Problem follow up - PIP source change
Svn correlation
PHP syntax summary
Thinkphp6 implements a simple lottery system
A method of quickly transplanting library function code to register code by single chip microcomputer
Database design (I)
Be a hard worker from today on