gpt4 book ai didi

python - pandas dataframe 对列进行排序会引发索引上的 keyerror

转载 作者:行者123 更新时间:2023-12-02 20:26:55 31 4
gpt4 key购买 nike

我有以下数据框,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'

我没有在此 DataFrame 上显式设置索引,它来自列表理解:

    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)

最佳答案

sort_valuesby 关键字需要列名称,而不是实际的系列本身。所以,你想要:

In [23]: df.sort_values('snr')
Out[23]:
peaklatency snr
0 52.99 0.0
4 54.57 42.0
3 54.64 52.0
1 54.15 62.0
5 54.13 72.0
2 54.12 82.0

关于python - pandas dataframe 对列进行排序会引发索引上的 keyerror,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38810395/

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