生成数据帧的一系列计算,以提供 NaN 占行总数的百分比,如下所示:
data = df.isnull().sum()/len(df)*100
RecordID 0.000000
ContactID 0.000000
EmailAddress 0.000000
ExternalID 100.000000
Date 0.000000
Name 0.000000
Owner 67.471362
Priority 0.000000
Status 0.000000
Subject 0.000000
Description 0.000000
Type 0.000000
dtype: float64
我想做的是将其表示为seaborn中的热图sns.heatmap(data)
,吸引读者注意那些 100 和 67% 的读者,不幸的是我收到了这个错误
IndexError:条件和输入之间的形状不一致
(得到 (12, 1) 和 (12,))
完整回溯:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-17-05db696a3a9b> in <module>()
----> 1 sns.heatmap(data)
~\AppData\Local\Programs\Python\Python36-32\lib\site-packages\seaborn\matrix.py in heatmap(data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, linewidths, linecolor, cbar, cbar_kws, cbar_ax, square, xticklabels, yticklabels, mask, ax, **kwargs)
515 plotter = _HeatMapper(data, vmin, vmax, cmap, center, robust, annot, fmt,
516 annot_kws, cbar, cbar_kws, xticklabels,
--> 517 yticklabels, mask)
518
519 # Add the pcolormesh kwargs here
~\AppData\Local\Programs\Python\Python36-32\lib\site-packages\seaborn\matrix.py in __init__(self, data, vmin, vmax, cmap, center, robust, annot, fmt, annot_kws, cbar, cbar_kws, xticklabels, yticklabels, mask)
114 mask = _matrix_mask(data, mask)
115
--> 116 plot_data = np.ma.masked_where(np.asarray(mask), plot_data)
117
118 # Get good names for the rows and columns
~\AppData\Local\Programs\Python\Python36-32\lib\site-packages\numpy\ma\core.py in masked_where(condition, a, copy)
1934 if cshape and cshape != ashape:
1935 raise IndexError("Inconsistent shape between the condition and the input"
-> 1936 " (got %s and %s)" % (cshape, ashape))
1937 if hasattr(a, '_mask'):
1938 cond = mask_or(cond, a._mask)
IndexError: Inconsistent shape between the condition and the input (got (12, 1) and (12,))
我的研究是否在 numpy 广播规则方面遇到了很多障碍,或者是 3 年前的一个错误——这些都没有太大帮助。
一如既往地感谢。