我很难理解以下内容:
当给定列具有空值时过滤数据帧的行后,我无法创建直方图。
#!/usr/bin/python3
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame({'A' : [None,2,None,4],
'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
'D' : np.array([3] * 4,dtype='int32'),
'E' : pd.Categorical(["test","train","test","train"]),
'F' : 'foo' })
c='A'
df=df[df['A'].notnull()]
print (df)
plt.figure()
plt.hist(df[c])
plt.show()
输出结果为:
A C D E F
1 2.0 1.0 3 train foo
3 4.0 1.0 3 train foo
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/srv/logs/tiaki/sentryo/exps/tools/ttplot.py in <module>()
32
33 plt.figure()
---> 34 plt.hist(df[c])
35 plt.show()
36
/usr/lib/python3/dist-packages/matplotlib/pyplot.py in hist(x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, hold, **kwargs)
2894 histtype=histtype, align=align, orientation=orientation,
2895 rwidth=rwidth, log=log, color=color, label=label,
-> 2896 stacked=stacked, **kwargs)
2897 draw_if_interactive()
2898 finally:
/usr/lib/python3/dist-packages/matplotlib/axes/_axes.py in hist(self, x, bins, range, normed, weights, cumulative, bottom, histtype, align, orientation, rwidth, log, color, label, stacked, **kwargs)
5576 # Massage 'x' for processing.
5577 # NOTE: Be sure any changes here is also done below to 'weights'
-> 5578 if isinstance(x, np.ndarray) or not iterable(x[0]):
5579 # TODO: support masked arrays;
5580 x = np.asarray(x)
/usr/local/lib/python3.4/dist-packages/pandas/core/series.py in __getitem__(self, key)
599 key = com._apply_if_callable(key, self)
600 try:
--> 601 result = self.index.get_value(self, key)
602
603 if not is_scalar(result):
/usr/local/lib/python3.4/dist-packages/pandas/indexes/base.py in get_value(self, series, key)
2167 try:
2168 return self._engine.get_value(s, k,
-> 2169 tz=getattr(series.dtype, 'tz', None))
2170 except KeyError as e1:
2171 if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:
pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3342)()
pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3045)()
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:4028)()
pandas/src/hashtable_class_helper.pxi in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:8146)()
pandas/src/hashtable_class_helper.pxi in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:8090)()
KeyError: 0
我做了另一次尝试,A
为[1,2,None,4]
,这很好用。
如果索引hist
不在0
中,那么df['A']
是否无法正常工作?