Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 选择表中某个值周围的行_Python_Pandas - Fatal编程技术网

Python 选择表中某个值周围的行

Python 选择表中某个值周围的行,python,pandas,Python,Pandas,我有一个连续测量的数据帧,以偶发事件为标志: TimeIndex Event Value 0 NaN 4.099969 1 NaN 3.833528 2 NaN -1.335025 3 A 4.420085 4 NaN 4.508899 5 NaN 4.557383 6 B -3.377152 7

我有一个连续测量的
数据帧
,以偶发事件为标志:

TimeIndex  Event    Value
0          NaN     4.099969  
1          NaN     3.833528
2          NaN     -1.335025
3          A       4.420085
4          NaN     4.508899
5          NaN     4.557383
6          B       -3.377152
7          NaN     4.508899
8          NaN     -1.919803
9          A       2.18520
10         NaN     3.821221
11         C       0.922389
12         NaN     2.165784
我想要每个事件的平均值,但也需要事件发生前两个时间点和事件发生后两个时间点的平均值。类似的方法可能会奏效:

TimeIndex  Event    Value      Around_A  Around_B  Around_C
0          NaN     4.099969      NaN       NaN       NaN
1          NaN     3.833528      -2        NaN       NaN
2          NaN     -1.335025     -1        NaN       NaN
3          A       4.420085       0        NaN       NaN
4          NaN     4.508899       1        -2        NaN
5          NaN     4.557383       2        -1        NaN
6          B       -3.377152     NaN        0        NaN
7          NaN     4.508899      -2         1        NaN
8          NaN     -1.919803     -1         2        NaN
9          A       2.18520        0        NaN        2
10         NaN     3.821221       1        NaN       -1
11         C       0.922389       2        NaN        0
12         NaN     2.165784      NaN       NaN        1
但是:1)我不确定如何在不循环的情况下获取新列值;2)对于许多不同的事件(我已经有了)来说,追加一个新列很难处理

是否有一种更简单的方法来选择pandas中某个值周围的时间点/行,然后按时间点/行进行平均

我期望的输出是事件x在大约时间内的平均值(此处显示的虚拟表示法)

我建议:

In [26]:

print df
    TimeIndex Event     Value
0           0   NaN  4.099969
1           1   NaN  3.833528
2           2   NaN -1.335025
3           3     A  4.420085
4           4   NaN  4.508899
5           5   NaN  4.557383
6           6     B -3.377152
7           7   NaN  4.508899
8           8   NaN -1.919803
9           9     A  2.185200
10         10   NaN  3.821221
11         11     C  0.922389
12         12   NaN  2.165784

[13 rows x 3 columns]
In [27]:

df['Around_A']=np.nan
In [28]:

for i in range(-2,3):
    df['Around_A'][(df.Event=='A').shift(i).fillna(False)]=i
    #or df.ix[(df.Event=='A').shift(i).fillna(False), 'Around_A']=i
In [29]:

print df
    TimeIndex Event     Value  Around_A
0           0   NaN  4.099969       NaN
1           1   NaN  3.833528        -2
2           2   NaN -1.335025        -1
3           3     A  4.420085         0
4           4   NaN  4.508899         1
5           5   NaN  4.557383         2
6           6     B -3.377152       NaN
7           7   NaN  4.508899        -2
8           8   NaN -1.919803        -1
9           9     A  2.185200         0
10         10   NaN  3.821221         1
11         11     C  0.922389         2
12         12   NaN  2.165784       NaN

[13 rows x 4 columns]
你还没有完全明白你的最后一个问题,你是否能提供一个预期的结果

编辑 现在很清楚,我的方法是:

In [22]:

df=pd.read_clipboard()
df['Around_A']=np.nan
df['Around_B']=np.nan
df['Around_C']=np.nan
for i in range(-2,3):
    df.ix[(df.Event=='A').shift(i).fillna(False), 'Around_A']=i
    df.ix[(df.Event=='B').shift(i).fillna(False), 'Around_B']=i
    df.ix[(df.Event=='C').shift(i).fillna(False), 'Around_C']=i
Data=[]
for s in ['A', 'B', 'C']:
    _df=pd.DataFrame(df.groupby('Around_%s'%s).Value.mean())
    _df['Event']=s
    _df.index.name='AroundTime'
    Data.append(_df.reset_index())
print pd.concat(Data)[['Event', 'AroundTime', 'Value']]
  Event  AroundTime     Value
0     A          -2  4.171213
1     A          -1 -1.627414
2     A           0  3.302643
3     A           1  4.165060
4     A           2  2.739886
0     B          -2  4.508899
1     B          -1  4.557383
2     B           0 -3.377152
3     B           1  4.508899
4     B           2 -1.919803
0     C          -2  2.185200
1     C          -1  3.821221
2     C           0  0.922389
3     C           1  2.165780

[14 rows x 3 columns] 

额外的澄清非常有用。请参见编辑。不幸的是,我在范围(-2,3)内的I的
循环中得到了
MemoryError
。我有16种事件类型和2800880个时间点,这就是为什么我希望通过对整个向量应用公式/条件来实现这一点。我认为在这种情况下使用
apply
会很困难,因为它是有条件的(基于周围的单元格)。我想你们很多人都想为A,B周围的
生成单独的
数据帧。一旦获得这些数据,就可以使用
dropna()
删除包含
nan
s的行,这将使数据集更小,并可能完全避免内存问题。
In [22]:

df=pd.read_clipboard()
df['Around_A']=np.nan
df['Around_B']=np.nan
df['Around_C']=np.nan
for i in range(-2,3):
    df.ix[(df.Event=='A').shift(i).fillna(False), 'Around_A']=i
    df.ix[(df.Event=='B').shift(i).fillna(False), 'Around_B']=i
    df.ix[(df.Event=='C').shift(i).fillna(False), 'Around_C']=i
Data=[]
for s in ['A', 'B', 'C']:
    _df=pd.DataFrame(df.groupby('Around_%s'%s).Value.mean())
    _df['Event']=s
    _df.index.name='AroundTime'
    Data.append(_df.reset_index())
print pd.concat(Data)[['Event', 'AroundTime', 'Value']]
  Event  AroundTime     Value
0     A          -2  4.171213
1     A          -1 -1.627414
2     A           0  3.302643
3     A           1  4.165060
4     A           2  2.739886
0     B          -2  4.508899
1     B          -1  4.557383
2     B           0 -3.377152
3     B           1  4.508899
4     B           2 -1.919803
0     C          -2  2.185200
1     C          -1  3.821221
2     C           0  0.922389
3     C           1  2.165780

[14 rows x 3 columns]