所以我的问题在于准备一个Dataframe来使用pandas和seaborn创建一个热图。我的问题是,在将所有内容从对象转换为整数时,是否将NaN值保留为NaN,以便我可以像sns.heatmap(df,mask = df.isnull())那样绘制它
到目前为止,我正在做的是将数据输入到我创建的新DataFrame中,创建后看起来像这样(https://imgur.com/a/fEDcnoi)。
从那里,我使用如下代码将值插入新的DataFrame中:
start = 16
end = start + 10
dates = range(start,end)
for d in dates:
str(d)
for i, row in jfk10day.iterrows():
row[f'Apr/{d}/2019'] = jfk[jfk['Pick-up Date'] == f'Apr/{d}/2019'][jfk['Supplier']==i][jfk['Car Type'] == 'Compact']['Total Price'].min()
将数据作为类型对象输入数据框。完成的数据框看起来像https://imgur.com/3m41KtL。
现在从这里我知道我需要将数据类型更改为int / float以便使用sns.heatmap()进行绘制,但是当我尝试类似的操作时:
jfk10day = jfk10day.astype(int)
我得到了错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-76-45dab2567d52> in <module>
----> 1 jfk10day.astype(int)
/anaconda3/lib/python3.7/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
176 else:
177 kwargs[new_arg_name] = new_arg_value
--> 178 return func(*args, **kwargs)
179 return wrapper
180 return _deprecate_kwarg
/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
4999 # else, only a single dtype is given
5000 new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors,
-> 5001 **kwargs)
5002 return self._constructor(new_data).__finalize__(self)
5003
/anaconda3/lib/python3.7/site-packages/pandas/core/internals.py in astype(self, dtype, **kwargs)
3712
3713 def astype(self, dtype, **kwargs):
-> 3714 return self.apply('astype', dtype=dtype, **kwargs)
3715
3716 def convert(self, **kwargs):
/anaconda3/lib/python3.7/site-packages/pandas/core/internals.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
3579
3580 kwargs['mgr'] = self
-> 3581 applied = getattr(b, f)(**kwargs)
3582 result_blocks = _extend_blocks(applied, result_blocks)
3583
/anaconda3/lib/python3.7/site-packages/pandas/core/internals.py in astype(self, dtype, copy, errors, values, **kwargs)
573 def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
574 return self._astype(dtype, copy=copy, errors=errors, values=values,
--> 575 **kwargs)
576
577 def _astype(self, dtype, copy=False, errors='raise', values=None,
/anaconda3/lib/python3.7/site-packages/pandas/core/internals.py in _astype(self, dtype, copy, errors, values, klass, mgr, **kwargs)
662
663 # _astype_nansafe works fine with 1-d only
--> 664 values = astype_nansafe(values.ravel(), dtype, copy=True)
665 values = values.reshape(self.shape)
666
/anaconda3/lib/python3.7/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy)
707 # work around NumPy brokenness, #1987
708 if np.issubdtype(dtype.type, np.integer):
--> 709 return lib.astype_intsafe(arr.ravel(), dtype).reshape(arr.shape)
710
711 # if we have a datetime/timedelta array of objects
pandas/_libs/lib.pyx in pandas._libs.lib.astype_intsafe()
pandas/_libs/src/util.pxd in util.set_value_at_unsafe()
ValueError: cannot convert float NaN to integer
所以我想知道是否有一种方法可以编辑我的for循环,以便每个条目都作为一个int输入(原始数据框“ Total Price”已经是int),或者是否有一种方法可以转换新的数据框在跳过NaN值时键入int。我需要热图中的NaN值来表明供应商在那个特定日期没有提供任何产品。
提前感谢帮助人员,如果需要我提供更多信息,请告诉我!
答案 0 :(得分:2)
从pandas 0.24.0版开始,我们有了nullable integer
数据类型:
df = pd.DataFrame({'Col':[1.0, 2.0, 3.0, np.NaN]})
print(df)
Col
0 1.0
1 2.0
2 3.0
3 NaN
print(df.Col.astype('Int64'))
0 1
1 2
2 3
3 NaN
Name: Col, dtype: Int64