gpt4 book ai didi

python - 使用索引标签创建新的Pandas DataFrame(iloc错误: Flexible Type)

转载 作者:行者123 更新时间:2023-12-03 08:28:36 24 4
gpt4 key购买 nike

这几乎就是我想要做的:Creating new pandas DataFrame from existing DataFrame and index

我没有索引号,而是有一些标签试图通过...

伪代码:

want_these_indices = ['A1BG',
'A4GALT',
'AAAS',
'AADAT',
'AAK1',
'AAMP',
'AANAT',
'AARS',...]

sorted(DF.index)
['A1BG',
'A4GALT',
'AAAS',
'AACS',
'AADAT',
'AAGAB',
'AAK1',
'AAMP',
'AANAT',
'AARS',
'AARS2',...]

但是当我 DF2 = DF.iloc[want_these_indices]我得到
我什至尝试了 TypeError: cannot perform reduce with flexible type
我认为可能是某些索引是浮点数或某些东西,但它们都是字符串的事实。在 DF.iloc[want_these_indices,:]DF.index

最佳答案

Pandas 具有不同的索引选择机制,其中一些使用整数作为位置,而其他使用标签。您有一个基于字符串的索引,因此需要使用不是位置索引器的索引器。更具体地说,df.iloc是严格基于整数的索引。您将要改用df.loc

关于python - 使用索引标签创建新的Pandas DataFrame(iloc错误: Flexible Type),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33375836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com