列上的pandas dataframe sort在索引上引发keyerror

2024-05-17 09:03:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下数据帧,df

   peaklatency        snr
0        52.99        0.0
1        54.15  62.000000
2        54.12  82.000000
3        54.64  52.000000
4        54.57  42.000000
5        54.13  72.000000

我试图按snr排序:

df.sort_values(df.snr)

但这增加了

_convert_to_indexer(self, obj, axis, is_setter)
   1208                 mask = check == -1
   1209                 if mask.any():
-> 1210                     raise KeyError('%s not in index' % objarr[mask])
   1211 
   1212                 return _values_from_object(indexer)

KeyError: '[ inf  62.  82.  52.  42.  72.] not in index'

我没有明确地在这个数据框架上设置索引,它来自于列表理解:

    import pandas as pd
    d = []
    for run in runs:
        d.append({            
            'snr': run.periphery.snr.snr,
            'peaklatency': (run.brainstem.wave5.wave5.argmax() / 100e3) * 1e3
        })
    df = pd.DataFrame(d)

Tags: 数据runindfindex排序notmask