简体   繁体   English

如何在应用 function 中使用 df.astype()

[英]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

pandas <= 0.25 pandas <= 0.25

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

pandas >= 1.0 pandas >= 1.0

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 ,它还允许您进行错误操作、 coerceraiseignore 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.

相关问题 如何在使用 df.astype() 时保留原始 pandas dataframe 值? 我需要为下面的示例提出一个值错误 - How do I keep original pandas dataframe values while using df.astype() ? I need to raise a value error for below example 使用df.astype的熊猫错误 - pandas error using df.astype Pandas df.astype(&#39;float32&#39;) 失去了很多精度 - Pandas df.astype('float32') loses a lot of precision pandas 未将 object dtype 转换为 float64,即使在 df.astype('float64') 无错误执行后也是如此 - pandas not converting an object dtype to float64 even after error free execution of df.astype('float64') 我如何使用 df 的多列作为 function 的输入? - how do i use multiple columns of a df as input to a function? 如何获取 apply function of pandas 中的所有 df 数据 - How to get all df data inside apply function of pandas 类型错误:无法将系列转换为<class 'float'> / 如何将 pandas df 应用于类对象函数? - TypeError: cannot convert the series to <class 'float'> / How do I apply a pandas df to a class object function? 我如何将语言工具应用于 Python df 并将结果添加为 df 中的新列? - how do i apply language tool to Python df and add results as new column in df? 如何在需要两个向量的函数中使用应用程序 - How do I use apply in pandas with a function that requires two vectors 如何使用 Dataframe 应用函数来规范化数据 - How do I use Dataframe apply function to normalize Data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM