我有一个如下所示的DataFrame:
-300 -298 -296
Time (ms)
Fp1 -0.416809 -0.024629 0.352019
Fp2 0.369807 0.402025 0.435756
F7 -0.822426 -0.895215 -0.973714
F3 -1.045553 -1.098616 -1.161518
Fz -0.477956 -0.462559 -0.450041
F4 0.779856 0.741989 0.693144
F8 0.602296 0.611537 0.630304
T3 0.260055 0.079092 -0.118850
C3 -1.568992 -1.612172 -1.668237
Cz -0.803537 -0.912913 -1.028954
C4 0.071638 0.162218 0.250011
T4 0.569891 0.547318 0.530495
T5 -0.390893 -0.416855 -0.435103
P3 -0.552106 -0.633048 -0.724976
Pz 0.038836 -0.147223 -0.342887
P4 -0.043076 -0.075429 -0.114958
T6 1.018918 1.086329 1.155682
O1 1.623878 1.332667 1.046043
O2 0.085277 -0.191445 -0.459685
行和列索引没问题:
In [111]: list(df)
Out[111]: [-300, -298, -296]
In [112]: print df.index.tolist()
[u'Fp1', u'Fp2', u'F7', u'F3', u'Fz', u'F4', u'F8', u'T3', u'C3', u'Cz', u'C4', u'T4', u'T5', u'P3', u'Pz', u'P4', u'T6', u'O1', u'O2']
访问单列范围可以正常工作:
In [109]: df.loc["Fp1":"F4", -300]
Out[109]:
Time (ms)
Fp1 -0.416809
Fp2 0.369807
F7 -0.822426
F3 -1.045553
Fz -0.477956
F4 0.779856
Name: -300, dtype: float64
但是当我尝试选择多行和多列范围时,我收到错误:
In [114]: df.loc["Fp1":"F4", -300:-296]
...
TypeError: cannot do slice indexing on <class 'pandas.core.indexes.base.Index'> with these indexers [-300] of <type 'int'>
嗯,问题是什么?
答案 0 :(得分:0)
正如DMZ所指出的,看起来人们不能使用负数进行索引,因此完成任务的最佳方法是将索引转换为字符串:
df.columns=[str(x) for x in df.columns]
然后像这样使用它们
df.loc["Fp1":"F4", "-300":"-296"]