使用索引标签创建新的Pandas DataFrame(iloc错误:灵活类型)



这正是我想要做的:从现有的DataFrame创建新的Panda DataFrame并索引

我没有索引号,我有一些标签,我正试图通过…进行索引

伪码:

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.indexwant_these_indices中,

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

最新更新