Skip to main content
 首页 » 编程设计

python之Ipython Notebook 不在数据框中打印 type()

2025年05月04日20Renyi-Fan

这是一个非常奇怪的错误:

以下代码:

a = 'string1' 
b = 'string2' 
 
test_dict = {'col1':{a:type(a), b:type(b)},'col2':{a:type(a), b:type(b)}} 
pd.DataFrame(test_dict) 

在普通的 ipython 控制台中,会按预期产生以下内容:

                col1          col2 
string1  <type 'str'>  <type 'str'> 
string2  <type 'str'>  <type 'str'> 

但是在 ipython notebook 中,应该显示类型的单元格是空的:

请您参考如下方法:

我怀疑如果您在浏览器中执行 View->Source,您将看到预期的 。发生的事情是浏览器认为这是一个 HTML 标签,没有识别它,只是把它扔掉了。

注意:如果我没有在这个答案中输入 <type ...>,我的答案也会发生同样的事情。