从Pandas DataFrame中提取多个非连续索引值

Nic*_*llo 3 python pandas

我已经创建了一个pandas数据框,通过以下方式从scipy.io读取它(file.sav是在不同的机器上创建的IDL结构.scipy.io创建一个标准的python字典):

from scipy import io
import pandas as p
import numpy as np
tmp=io.readsav('file.sav', python_dict = True)
df=pd.DataFrame(tmp,index=tmp['shots'].astype('int32'))
Run Code Online (Sandbox Code Playgroud)

数据帧包含了一组值(从file.sav)和作为指数一系列形式19999,20000,30000等的整数现在我想借这些指数的一个子集,说:

df.loc[[19999,20000]]
Run Code Online (Sandbox Code Playgroud)

由于某些原因,我得到表格的错误

raise ValueError('Cannot index with multidimensional key')
Run Code Online (Sandbox Code Playgroud)

加上其他和最后

ValueError: Big-endian buffer not supported on little-endian compiler
Run Code Online (Sandbox Code Playgroud)

但是我已经检查过我正在处理的机器和创建了file.sav的机器都是小端.所以我认为这不是问题所在.

Jef*_*eff 5

您的输入文件是大端.看到这里改变它:http://pandas.pydata.org/pandas-docs/dev/gotchas.html#byte-ordering-issues

比较之前和之后

In [7]: df.dtypes
Out[7]: 
a        >f4
b        >f4
c        >f4
shots    >f4
dtype: object

In [9]: df.apply(lambda x: x.values.byteswap().newbyteorder())
Out[9]: 
<class 'pandas.core.frame.DataFrame'>
Int64Index: 100 entries, 20000 to 20099
Data columns (total 4 columns):
a        100  non-null values
b        100  non-null values
c        100  non-null values
shots    100  non-null values
dtypes: float32(4)

In [10]: df.apply(lambda x: x.values.byteswap().newbyteorder()).dtypes
Out[10]: 
a        float32
b        float32
c        float32
shots    float32
dtype: object
Run Code Online (Sandbox Code Playgroud)

在你执行此操作后也设置索引(例如,不要在构造函数中执行此操作)

df.set_index('shots',inplace=True)
Run Code Online (Sandbox Code Playgroud)