2

我有一个pandas dataframe列:

点击值的“视频”和“链接”

带有日期时间的索引。出于某种原因,当我在视频系列中使用符号学和箱线图时,我得到了错误

ValueError: Data has no positive values, and therefore can not be log-scaled.

但是当我在“链接”系列上这样做时,我可以正确绘制箱线图。

我已经验证“视频”和“链接”系列都具有 NaN 值和正值。

关于为什么会发生这种情况的任何想法?以下是我为验证情况所做的工作

下面是示例代码:

#get all the not null values of video to show that there are positive
temp=a.types_pivot[a.types_pivot['video'].notnull()]
print temp

#get a count of all the NaN values to show both 'video' and 'link' has NaN
count = 0 
for item in a.types_pivot['video']:
    if(item.is_integer() == False):
        count += 1

#try to draw the plots
print "there is %s nan values in video" % (count)

fig=plt.figure(figsize=(6,6),dpi=50)
ax=fig.add_subplot(111)
ax.semilogy()
plt.boxplot(a.types_pivot['video'].values)

这是视频系列代码的相关输出

    输入链接视频
    创建时间
2011-02-10 15:00:51+00:00 NaN 5 2011-02-17 17:50:38+00:00 NaN 5 2011-03-22 14:04:56+00:00 NaN 5

视频中有 5463 个 nan 值

我运行相同的确切代码,除了我这样做

a.types_pivot['link'] 

我能够绘制箱线图。

以下是链接系列的相关输出

索引:5269 个条目,2011-01-24 20:03:58+00:00 到 2012-06-22 16:56:30+00:00
数据列:
链接 5269 个非空值
照片 0 非空值
问题 0 非空值
状态 0 非空值
swf 0 非空值
视频 0 非空值
数据类型:float64(6)

链接中有 216 个 nan 值

Using the describe function

a.types_pivot['video'].describe()

<pre>
count    22.000000
mean     16.227273
std      15.275040
min       1.000000
25%       5.250000
50%       9.500000
75%      23.000000
max      58.000000
</pre>
4

1 回答 1

1

注意:由于 imgur 的一些问题,我无法上传图片。我稍后再试。

通过调用 pd.DataFrame.boxplot() 来利用 pandas matplotlib 助手/包装器。我相信这将为您处理 NaN 值。它还将两个系列放在同一个图中,以便您轻松比较数据。

示例 创建一个包含一些 NaN 值和负值的数据框

In [7]: df = pd.DataFrame(np.random.rand(10, 5))    
In [8]: df.ix[2:4,3] = np.nan
In [9]: df.ix[2:3,4] = -0.45
In [10]: df
Out[10]: 
          0         1         2         3         4
0  0.391882  0.776331  0.875009  0.350585  0.154517
1  0.772635  0.657556  0.745614  0.725191  0.483967
2  0.057269  0.417439  0.861274       NaN -0.450000
3  0.997749  0.736229  0.084077       NaN -0.450000
4  0.886303  0.596473  0.943397       NaN  0.816650
5  0.018724  0.459743  0.472822  0.598056  0.273341
6  0.894243  0.097513  0.691781  0.802758  0.785258
7  0.222901  0.292646  0.558909  0.220400  0.622068
8  0.458428  0.039280  0.670378  0.457238  0.912308
9  0.516554  0.445004  0.356060  0.861035  0.433503

请注意,我可以像这样计算 NaN 值的数量:

In [14]: df[3].isnull().sum()   # Count NaNs in the 4th column
Out[14]: 3

箱线图很简单:

In [16]: df.boxplot()

您可以创建一个半对数箱线图,例如,通过:

In [23]: np.log(df).boxplot()

或者,更一般地说,修改/转换为您心中的内容,然后是箱线图。

In [24]: df_mod = np.log(df).dropna()    
In [25]: df_mod.boxplot()
于 2012-10-31T01:44:35.737 回答