Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 转换为浮点以获取前10个最大值时出错_Python_Python 3.x_Pandas_Data Science - Fatal编程技术网

Python 转换为浮点以获取前10个最大值时出错

Python 转换为浮点以获取前10个最大值时出错,python,python-3.x,pandas,data-science,Python,Python 3.x,Pandas,Data Science,我尝试使用NLAGEST函数返回前10个值,代码如下: df['roi'].astype(float).nlargest(3, 'roi') 但是得到一个错误 ValueError: keep must be either "first", "last" or "all" roi列是一个对象,这就是为什么我使用astype float,但仍然得到一个错误 当我在nlargest函数中尝试keep=all或keep=first或las

我尝试使用NLAGEST函数返回前10个值,代码如下:

df['roi'].astype(float).nlargest(3, 'roi')
但是得到一个错误

ValueError: keep must be either "first", "last" or "all"
roi列是一个对象,这就是为什么我使用astype float,但仍然得到一个错误

当我在nlargest函数中尝试keep=all或keep=first或last过滤器时,我得到一个
TypeError的错误:nlargest()为参数“keep”得到了多个值。


谢谢

若要使用所需的方法,必须将代码更改为:

df.astype(float).nlargest(3, 'roi')
因为此语法仅适用于
pandas.DataFrames
。如果您想像在字典中一样通过键指定列,那么您将使用
pandas.Series
,正确的语法是

df['roi'].astype(float).nlargest(3)

这两种方法的文档都是,对于DataFrames,对于Series

,若要使用所需的方法,必须将代码更改为:

df.astype(float).nlargest(3, 'roi')
因为此语法仅适用于
pandas.DataFrames
。如果您想像在字典中一样通过键指定列,那么您将使用
pandas.Series
,正确的语法是

df['roi'].astype(float).nlargest(3)

这两种方法的文档都是,对于数据帧,对于系列,对于单行程序,您需要首先将“roi”转换为浮点类型,然后执行大的操作:

将字典传递给
.astype
允许我们返回整个数据帧,对特定列的数据类型进行选择性更改,然后我们可以对返回的
数据帧执行
.nlargest
(而不仅仅是拥有
系列


对于单行程序,您需要首先将“roi”转换为浮点类型,然后执行
nlargest

将字典传递给
.astype
允许我们返回整个数据帧,对特定列的数据类型进行选择性更改,然后我们可以对返回的
数据帧执行
.nlargest
(而不仅仅是拥有
系列


你试过这个吗
roi
不是有效选项。是的,当我尝试keep=all或keep=first或last参数时,我得到一个类型错误error:nlargest()为参数“keep”获得了多个值,当您选择一个列时
df['roi']
您得到了一个系列,并且您想使用它了吗
roi
不是一个有效选项。是的,当我尝试keep=all或keep=first或last参数时,我得到一个类型错误error:nlargest()为参数“keep”得到了多个值当你选择一个列
df['roi']
你得到了一个系列,你想使用感谢,答案给了我一个系列输出,只有roi列,我可以得到整个df的df,而不是df中的其他列吗?您可以先修复列
df['roi']=df['roi'].astype(float)
,然后执行
df.nlargest(3,'roi')
@Chris90,我不明白。您想将
nlargest
应用于
'roi'
列并获得一个数据帧,还是想将
nlargest
应用于整个数据帧?@Ralubrusto嗨,我想将nlargest应用于我的df,但也希望输出也是df啊,所以您只需要使用第一个命令
df.astype(float).nlargest(3,‘roi’)
谢谢,答案给了我一系列roi列的输出,我是否可以得到整个df的df,而不是df中的其他列?你可以先修复列
df['roi']=df['roi']。astype(float)
然后执行
df.nlargest(3,‘roi’)
@Chris90我不明白。您想将
nlargest
应用于
'roi'
列并获得一个数据帧,还是想将
nlargest
应用于整个数据帧?@Ralubrusto嗨,我想将nlargest应用于我的df,但也希望输出也是df啊,所以您只需要使用第一个命令<代码>df.astype(float).nlargest(3,'roi')