仅针对非零值运行df.describe()

时间:2017-11-28 23:30:44

标签: python pandas dataframe statistics

我的数据框daily看起来像这样

import pandas as pd
daily

time_stamp  22          72      79          86      87          88          90  
2013-10-01  0.000000    0.000   8.128000    0.254   0.000000    0.000000    0.000000
2013-10-01  0.000000    0.000   8.128000    0.254   0.000000    0.000000    0.000000
2013-10-02  0.000000    0.000   0.000000    0.000   0.000000    0.000000    0.000000
2013-10-04  0.000000    0.000   0.000000    0.000   2.540000    0.762000    0.000000
2013-10-08  2.286000    0.000   0.000000    1.016   1.016000    0.254000    0.000000
2013-10-11  2.794000    0.000   0.000000    0.000   3.810000    1.016000    0.762000
2013-10-12  1.524000    0.000   0.000000    2.286   5.588000    0.254000    26.41600
2013-10-13  0.762000    0.000   8.890000    0.000   2.540000    1.270000    4.572000
2013-10-14  1.524000    0.000   0.000000    0.000   2.540000    4.064000    0.000000
2013-10-15  0.000000    0.000   0.000000    0.000   0.000000    0.000000    0.000000
2013-10-16  0.000000    3.810   1.524000    3.048   0.508000    0.762000    5.080000
2013-10-17  0.000000    0.000   0.254000    0.000   0.000000    0.000000    0.508000
2013-10-18  8.128000    0.762   4.826000    0.508   7.366000    4.572000    1.524000
2013-10-19  8.382000    0.254   0.000000    0.000   6.858000    16.510000   2.032000
2013-10-20  0.000000    0.000   0.000000    0.000   4.064000    5.842000    0.000000
2013-10-21  0.000000    0.508   0.000000    0.000   1.016000    0.000000    0.000000
2013-10-22  2.794000    2.540   1.016000    0.000   0.508000    15.748000   0.000000

我想对大于0的值进行汇总统计,describe()

问题是如果我使用命令dailyrf = daily[(daily > 0.).any(1)],当我执行dailyrf.describe()时,仍会包含带零的行。或者,当我执行dailyrf = daily[(daily > 0.).all(1)]时,它仅返回在所有行中具有> 0值的行。

我还尝试了daily[daily==0] = 'NaN',它给了我一条警告信息:“正在尝试在DataFrame的切片副本上设置值。 尝试使用.loc [row_indexer,col_indexer] = value而不是

请参阅文档中的警告:http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy   这与ipykernel包是分开的,因此我们可以避免在“。

之前进行导入

这不是解决方案,因为describe函数返回此:

        22  72  79  86  87  88  90  93  95  96  97
count   720 684 721 719 718 720 720 721 720 720 719
unique  103 80  73  64  80  108 112 108 86  113 98
top     NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
freq    470 494 560 510 539 483 486 441 570 474 476

我真正想要的是每列中大于0的所有值的均值,标准偏差等。

1 个答案:

答案 0 :(得分:2)

修复代码通知NaN!='NaN'

df[df==0] = np.nan
df.describe()
Out[696]: 
             22        72        79        86         87         88        90
count  8.000000  5.000000  7.000000  6.000000  12.000000  11.000000   7.00000
mean   3.524250  1.574800  4.680857  1.227667   3.196167   4.641273   5.84200
std    3.000573  1.538745  3.752722  1.174092   2.391229   5.992560   9.24574
min    0.762000  0.254000  0.254000  0.254000   0.508000   0.254000   0.50800
25%    1.524000  0.508000  1.270000  0.317500   1.016000   0.762000   1.14300
50%    2.540000  0.762000  4.826000  0.762000   2.540000   1.270000   2.03200
75%    4.127500  2.540000  8.128000  1.968500   4.445000   5.207000   4.82600
max    8.382000  3.810000  8.890000  3.048000   7.366000  16.510000  26.41600