Python 将类 'pandas.indexes.numeric.Int64Index' 转换为 numpy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37958278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:09:26  来源:igfitidea点击:

Convert class 'pandas.indexes.numeric.Int64Index' to numpy

pythonnumpypandasdataframe

提问by Chris Parry

I am isolating some row ids from a Pandas dataframe, like this:

我正在从 Pandas 数据框中隔离一些行 ID,如下所示:

data = df.loc[df.cell == id]
rows = df.index

print(type(rows))
< class 'pandas.indexes.numeric.Int64Index'>

I want to convert rows to a numpy array so I can save it to a mat file using sio.savemat. This is returning an error though:

我想将行转换为 numpy 数组,以便可以使用 sio.savemat 将其保存到 mat 文件中。这虽然返回一个错误:

row_mat = rows.as_matrix()
AttributeError: 'Int64Index' object has no attribute 'as_matrix'

What is the correct way, please? Thanks

请问正确的做法是什么?谢谢

回答by user3404344

try rows = df.index.valuesinstead

尝试rows = df.index.values代替