[英]How do I use df.astype() inside apply function
I have a data frame in which all the data in columns are of type object.我有一个数据框,其中列中的所有数据都是 object 类型。 Now I want to convert all objects into numeric types using astype() function but I don't want to do something like this ->
现在我想使用 astype() function 将所有对象转换为数字类型,但我不想做这样的事情->
df.astype({'col1': 'int32', 'col2': 'int32'....})
If I do something like this ->如果我做这样的事情->
I get an error because apply function needs Series to traverse.我收到一个错误,因为 apply function 需要 Series 遍历。
PS: The other option of doing the same thing is -> PS:做同样事情的另一个选择是->
df.apply(pd.to_numeric)
But I want to do this using.astype() Is there any other way instead of using df.apply() and still convert all object type data into numeric using df.astype()但是我想使用.astype() 来做这个有没有其他方法可以代替使用 df.apply() 并且仍然使用 df.astype() 将所有 object 类型数据转换为数字
Use df = df.astype(int)
to convert all columns to int datatype使用
df = df.astype(int)
将所有列转换为 int 数据类型
import numpy
df.astype(numpy.int32)
If these are object
columns and you're certain they can be "soft-casted" to int, you have two options:如果这些是
object
列,并且您确定它们可以“软转换”为 int,那么您有两个选择:
df
worker day tasks
0 A 2 read
1 A 9 write
2 B 1 read
3 B 2 write
4 B 4 execute
df.dtypes
worker object
day object
tasks object
dtype: object
infer_objects
(0.21+ only) casts your data to numpy types if possible.如果可能,
infer_objects
(仅限 0.21+)将您的数据转换为 numpy 类型。
df.infer_objects().dtypes
worker object
day int64
tasks object
dtype: object
convert_dtypes
casts your data to the most specific pandas extension dtype if possible.如果可能,
convert_dtypes
会将您的数据转换为最具体的 pandas 扩展数据类型。
df.convert_dtypes().dtypes
worker string
day Int64
tasks string
dtype: object
Also see this answer by me for more information on "hard" versus "soft" conversions.有关“硬”与“软”转换的更多信息,另请参阅我的这个答案。
In my opinion the safest is to use pd.to_numeric
in your apply
function which also allows you error manipulation, coerce
, raise
or ignore
.在我看来,最安全的方法是在您的
apply
程序 function 中使用pd.to_numeric
,它还允许您进行错误操作、 coerce
、 raise
或ignore
。 After getting the columns to numeric, then you can safely perform your astype()
operation, but I wouldn't suggest it to begin with:将列转换为数字后,您可以安全地执行
astype()
操作,但我不建议以以下方式开始:
df.apply(pd.to_numeric, errors='ignore')
If the column can't be converted to numeric, it will remain unchanged如果该列不能转换为数字,它将保持不变
df.apply(pd.to_numeric, errors='coerce')
The columns will be converted to numeric, the values that can't be converted to numeric in the column will be replaced with NaN
.列将转换为数字,列中无法转换为数字的值将替换为
NaN
。
df.apply(pd.to_numeric, errors='raise')
ValueError
will be returned if the column can't be converted to numeric如果无法将列转换为数字,将返回
ValueError
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.