如何使用 Bokeh 0.10.0 处理梯度交叉表的缺失值?
How to handle missing value for gradient crosstab using Bokeh 0.10.0?
我是新手,正在使用 Bokeh 0.10.0,遵循此 example。
我在 pandas df 中引入缺失值
# Swap a real numeric value to missing
data['Jan'][0] = np.nan
行后
data = data.set_index('Year')
运行时报错
Traceback (most recent call last):
File "C:\Users\KubiK\Desktop\Try2.py", line 36, in <module>
color.append(colors[min(int(monthly_rate)-2, 8)])
ValueError: cannot convert float NaN to integer
我们如何告诉 Bokeh 跳过那个缺失值?
我看到两个可能的选择。
[选项 1] 预先对 pandas DataFrame data
进行替换,并在 for 循环中处理颜色分配:
data.replace([np.nan], -1, inplace=True)
for y in years:
for m in months:
month.append(m)
year.append(y)
monthly_rate = data[m][y]
if monthly_rate == -1:
color.append('#FFFFFF')
rate.append(monthly_rate)
color.append(colors[min(int(monthly_rate)-2, 8)])
[选项 2] 使用 if
.
处理 for 循环中的 np.nan
for y in years:
for m in months:
month.append(m)
year.append(y)
monthly_rate = data[m][y]
if np.isnan(monthly_rate):
rate.append(-1)
color.append('#FFFFFF')
else:
rate.append(monthly_rate)
color.append(colors[min(int(monthly_rate)-2, 8)])
请注意,我正在分配颜色 #FFFFFF
和 -1
的值,但您可以将其更改为您想要的值。
我是新手,正在使用 Bokeh 0.10.0,遵循此 example。
我在 pandas df 中引入缺失值
# Swap a real numeric value to missing
data['Jan'][0] = np.nan
行后
data = data.set_index('Year')
运行时报错
Traceback (most recent call last):
File "C:\Users\KubiK\Desktop\Try2.py", line 36, in <module>
color.append(colors[min(int(monthly_rate)-2, 8)])
ValueError: cannot convert float NaN to integer
我们如何告诉 Bokeh 跳过那个缺失值?
我看到两个可能的选择。
[选项 1] 预先对 pandas DataFrame data
进行替换,并在 for 循环中处理颜色分配:
data.replace([np.nan], -1, inplace=True)
for y in years:
for m in months:
month.append(m)
year.append(y)
monthly_rate = data[m][y]
if monthly_rate == -1:
color.append('#FFFFFF')
rate.append(monthly_rate)
color.append(colors[min(int(monthly_rate)-2, 8)])
[选项 2] 使用 if
.
np.nan
for y in years:
for m in months:
month.append(m)
year.append(y)
monthly_rate = data[m][y]
if np.isnan(monthly_rate):
rate.append(-1)
color.append('#FFFFFF')
else:
rate.append(monthly_rate)
color.append(colors[min(int(monthly_rate)-2, 8)])
请注意,我正在分配颜色 #FFFFFF
和 -1
的值,但您可以将其更改为您想要的值。