- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
astype
在使用 dict of columns
时引发 ValueError
。
我正在尝试将大 DF 中的稀疏列类型转换(从 float 到 int)。我的问题是 NaN
值。即使 errors
参数设置为 'ignore'
,在使用列字典时也不会忽略它们。
这是一个玩具示例:
t=pd.DataFrame([[1.01,2],[3.01, 10], [np.NaN,20]])
t.astype({0: int}, errors='ignore')
ValueError: Cannot convert non-finite values (NA or inf) to integer
最佳答案
您可以使用新的 nullable integer Pandas 0.24.0+ 中的 dtype。在使用 astype
之前,您首先需要将任何不完全等于整数的 float 转换为等于整数值(例如舍入、截断等):
In [1]: import numpy as np; import pandas as pd; pd.__version__
Out[1]: '0.24.2'
In [2]: t = pd.DataFrame([[1.01, 2],[3.01, 10], [np.NaN, 20]])
In [3]: t.round().astype('Int64')
Out[3]:
0 1
0 1 2
1 3 10
2 NaN 20
关于python - DataFrame.astype() 错误参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56169473/
我有一个 DataFrame,其中一列由浮点型对象组成。我想转换为 int。正如我所尝试的,.astype("int") 和 astype(int) 方法都有效。我只是想知道是否有任何区别? 最佳答案
我有一个简单的数据框 df: col1 | col2 7 | 8 12 | 14 当我通过写 df.dtypes 检查 df 的数据类型时,int64 是两列的数据类型。 现在我要通过键
我目前正在使用 Anaconda 的 Spyder,我正在尝试将包含 float 类型的数组转换为 int 类型: x = np.array([1, 2, 2.5]) x.astype(int) pr
使用此简单的代码行,我会不断遇到SettingWithCopyWarning错误,而不是整个代码。 #make email a string df['Email Address'] = df['Ema
给定一系列类型 object我想知道使用 .astype() 可以成功将多少元素转换为给定类型功能。 例如,尝试将以下系列转换为 float32 : s = pd.Series([1.1, 'foo'
我正在使用 timedeltas,似乎这段代码 copy_for_U.Time.astype('timedelta64[m]',copy=False); 不会更改数据框 - 如果我从文档中正确理解,它
看来 Groovy 中转换对象的约定是使用 as 运算符并覆盖 asType()。例如: class Id { def value @Override public Objec
考虑以下示例: df = pd.DataFrame([[1, "a"], [2, "b"]], columns=["int", "str"]) df.astype({"int":np.int8, "s
astype 在使用 dict of columns 时引发 ValueError。 我正在尝试将大 DF 中的稀疏列类型转换(从 float 到 int)。我的问题是 NaN 值。即使 errors
我正在尝试在数据框的列中查找中值。我得到的中值是 float ,但我需要它的整数格式。 c_med = round(df['count'].median().astype(int)) c_med =
我的DataFrame中有一列float64,需要将其转换为Int。我尝试了这两个代码,但数据类型保持不变(float64) final_df['Total'] = final_df.Total.as
所以我试图将我的 float numpy 数组转换为整数。但是当我这样做时: array.astype(np.uint64) 它改变了这个数组: [ 550. 514. 451. 494. 4
使用astype实现dataframe字段类型转换 ? 1
我尝试使用 round 然后 astype 将浮点 DataFrame 的某些列中的值转换为整数。但是,这些值仍然包含小数位。我的代码有什么问题? nums = np.arange(1, 11) ar
用Delphi编写 uses System.Classes; ... var A: TAlignment; Value: TValue; begin Value := 0; A :=
我正在尝试使用 RTTI 将事件处理程序添加到控件,该控件可能已经设置了事件处理程序。代码看起来像这样: var prop: TRttiProperty; val: TValue; begin
我有一个数字列,它可以包含另一个不同形式的字符 [0-9] .说:x = pandas.Series(["1","1.2", "*", "1", "**."]) . 然后我想使用 x.astype(d
当我尝试将 astype(float) 方法应用于 pandas 中的分组数据框时,出现以下错误。 ValueError: could not convert string to float: 你
我正在使用 geopy 包搜索地址的坐标,列返回匹配的地址和坐标 我只想获取坐标 这是一个向您展示其工作原理的测试: # Test to see if response is obtained for
我有一个二维 numpy 数组,我想将特定的 dtype 应用于每一列。 a = np.arange(25).reshape((5,5)) In [40]: a Out[40]: array([[ 0
我是一名优秀的程序员,十分优秀!