dropna()标签问题

时间:2013-09-26 11:01:59

标签: python csv pandas average

我试图在熊猫中平均一组数据。来自csv文件的数据。 我有一个名为'track'的系列。在早期阶段,我使用方法dropna()来删除在读取csv文件时导入的一些空白行。

我使用的方法是平均超过5行的列。我不能使用rolling_mean方法,因为我想使用当前值之前的两行,当前值和当前值之后的两行取平均值。

当我到达已删除NaN数据的数据时,我遇到了问题,因为标签也已经去了。

def get_data(filename):
    '''function to read the data form the input csv file to use in the analysis'''
    with open(filename, 'r') as f:
        reader = pd.read_csv(f, sep=',', usecols=('candidate',' final track' ,' status'))                      
    print reader[0:20]            
    reader=reader.dropna()
    print reader[0:20]
    return reader 

def relative_track(nb):

    length= len(reader) 
    track=current_tracks.loc[:,' final track']
    for el in range(2, length):
        means=pd.stats.moments.rolling_mean(track, 5)
        print means

这给出了输出(第二次打印时缺少15,16的注释标签):

                candidate   final track  status
0                       1           719       *
1                       2           705       *
2                       3           705       *
3                       4           706       *
4                       5           704       *
5                       1           708       *
6                       2           713       *
7                       3           720       *
8                       4           726       *
9                       5           729       *
10                      1           745       *
11                      2           743       *
12                      3           743       *
13                      4           733       *
14                      5           717       *
15                    NaN           NaN     NaN
16  *** Large track split           NaN     NaN
17                      1           714       *
18                      2           695       *
19                      3           690       *
   candidate   final track  status
0          1           719       *
1          2           705       *
2          3           705       *
3          4           706       *
4          5           704       *
5          1           708       *
6          2           713       *
7          3           720       *
8          4           726       *
9          5           729       *
10         1           745       *
11         2           743       *
12         3           743       *
13         4           733       *
14         5           717       *
17         1           714       *
18         2           695       *
19         3           690       *
20         4           671       *
21         5           657       *

但是当我尝试使用第二个函数计算均值时,我得到错误:

    raise KeyError("stop bound [%s] is not in the [%s]" % (key.stop,self.obj._get_axis_name(axis)))
KeyError: 'stop bound [15] is not in the [index]'

这是因为索引15不存在。如果有人能提供帮助那就太棒了。

1 个答案:

答案 0 :(得分:0)

  

我不能使用rolling_mean方法,因为我想在当前值,当前值和当前值之后的两行之前使用两行取平均值。

使用关键字参数center=True,在this section of the documentation末尾描述。

此外,pd.stats.moments.rolling_mean可以简单地作为pd.rolling_mean访问;它是熊猫的顶级功能。

P.S。我想我在这里理解你的意图,但你的代码可能有一些与你的问题无关的问题。 (例如,最后一个for循环中的el计数变量没有被使用 - 看起来它只是反复做同样的事情。)但也许center关键字可以消除你现有的大部分工作反正。