【发布时间】:2018-10-05 01:49:22
【问题描述】:
我在下面有以下数据框:
Rec Channel Value1 Value2
Pre 10 20
Pre 35 42
Pre 42 90
Event A 23 39
FF 50 75
Post A 79 11
Post B 88 69
Post 55 20
Post 62 34
Post 43 21
使用以下代码:
idxall = df[df['Chan'].isin({'A', 'B'})]
idx1 = idxall.iloc[[0]] #First Occurrance
idx2 = idxall.iloc[[-1]] #Last Occurrance
我希望能够为第一个通道读数上方的 2 个实例以及最后一个实例下方的 2 个实例获取 Value1 列的一部分,如果我想根据以下内容读取它,那么语法将如何Value1 的列字符串?
期望的输出:
Value 1
35
42
23
50
79
88
55
62
我尝试使用:
val1 = df.loc[idx1-2: idx2+2, 'Value1']
print(val1)
但出现以下错误:TypeError: Could not operation 3 with block values unsupported operand type(s) for -: 'str' and 'int'
有人可以提供相应的建议吗?
【问题讨论】:
标签: python pandas dataframe slice