2015-10-20 132 views
1

任何人都可以請解釋爲什麼下面的輸出沒有數據?未生產Results.html帶CSS樣式的熊貓df.to_html

df = pd.read_csv(os.path.join(root,filename), skip_blank_lines=True) 
df.dropna(how="all", inplace=True) 
data = df.sort(ascending=True) 
HTML('''<style>.white_space_df td { white-space: normal; }</style>''') 
HTML(data.to_html(os.path.join(root, "Results.html"), index=False, na_rep="NA", classes='white_space_df')) 

更新

我能夠制定出自己的問題,只是包住別人遇到這下面是工作的例子。

df = pd.read_csv(os.path.join(root,filename), skip_blank_lines=True) 
df.dropna(how="all", inplace=True) 
data = df.sort(ascending=True) 
style = '''<style>.white_space_df td { word-wrap: break-word; }</style>''' 
style + data.to_html(os.path.join(root, "Results.html"), index=False, na_rep="NA", classes='white_space_df') 
+0

你確定你有df中的數據嗎? –

+0

Yip工作正常,如果我刪除'HTML'只有'data.to_html(os.path.join(root,「Results.html」),index = False,na_​​rep =「NA」)''但當然我試圖添加造型 – iNoob

+0

你正在使用的HTML功能究竟是什麼? –

回答

1

to_html方法(當通過了buf參數)節省的HTML文件,也就是說,它保存XML來Results.html ...並且該方法返回None

data.to_html(os.path.join(root, "Results.html"), index=False, na_rep="NA", classes='white_space_df') 

如果不通過一個buf參數(第一個),它返回一個字符串:

In [11]: df = pd.DataFrame([[1, 2], [3, 4]], columns=["A", "B"]) 

In [12]: df.to_html() 
Out[12]: '<table border="1" class="dataframe">\n <thead>\n <tr style="text-align: right;">\n  <th></th>\n  <th>A</th>\n  <th>B</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n  <th>0</th>\n  <td>1</td>\n  <td>2</td>\n </tr>\n <tr>\n  <th>1</th>\n  <td>3</td>\n  <td>4</td>\n </tr>\n </tbody>\n</table>' 

所以,你想這個字符串傳遞給HTML(而不是無):

HTML(data.to_html(index=False, na_rep="NA", classes='white_space_df')) 
+0

嗯我已經排序這個,我確實在我的帖子中發佈了更新,不知道哪裏去了。但我即將上牀睡覺,生病了仔細看看,一旦我不是殭屍明天錯誤今天錯誤後來。謝謝安迪 – iNoob