Bạn có thể sử dụng Javascript để mở cửa sổ mới, được thực hiện bởi HTML
từ IPython.display
.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(6,4),columns=list('ABCD'))
# Show in Jupyter
df
from IPython.display import HTML
s = '<script type="text/Javascript">'
s += 'var win = window.open("", "Title", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=780, height=200, top="+(screen.height-400)+", left="+(screen.width-840));'
s += 'win.document.body.innerHTML = \'' + df.to_html().replace("\n",'\\') + '\';'
s += '</script>'
# Show in new Window
HTML(s)
Đây, tạo một chuỗi HTML từ khung dữ liệu chứa nhiều dòng mới. Đó là vấn đề đối với Javascript. Chuỗi nhiều dòng trong Javascript yêu cầu dấu gạch chéo ngược tại EOL, đó là lý do tại sao python phải sửa đổi chuỗi HTML bằng phương thức .replace()
.
Điều thực sự mát mẻ với JavaScript của .innerHTML
(thay vì document.write()
) là bạn có thể cập nhật bảng của bạn bất cứ lúc nào mà không cần phải tạo ra một cửa sổ mới:
df /= 2
s = '<script type="text/Javascript">'
s += 'win.document.body.innerHTML = \'' + df.to_html().replace("\n",'\\') + '\';'
s += '</script>'
HTML(s)
này sẽ có hiệu lực ngay lập tức trên bàn của bạn trong mở cửa sổ.
Dưới đây là một gợi ý đơn giản của một View()
giả lập từ R
cho python
:
def View(df):
css = """<style>
table { border-collapse: collapse; border: 3px solid #eee; }
table tr th:first-child { background-color: #eeeeee; color: #333; font-weight: bold }
table thead th { background-color: #eee; color: #000; }
tr, th, td { border: 1px solid #ccc; border-width: 1px 0 0 1px; border-collapse: collapse;
padding: 3px; font-family: monospace; font-size: 10px }</style>
"""
s = '<script type="text/Javascript">'
s += 'var win = window.open("", "Title", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=780, height=200, top="+(screen.height-400)+", left="+(screen.width-840));'
s += 'win.document.body.innerHTML = \'' + (df.to_html() + css).replace("\n",'\\') + '\';'
s += '</script>'
return(HTML(s+css))
này hoạt động trong jupyter chỉ đơn giản bằng cách gõ:
View(df)
Là một topping lạ mắt, nó cũng tạo kiểu cho bảng đã mở của bạn bằng cách sử dụng một số CSS, sao cho nó trông đẹp hơn và c không thể so sánh với những gì bạn biết từ số RStudio
.
Bạn có thể thử sử dụng các tệp html thời gian như http://stackoverflow.com/questions/39977117/ipython-display-full-dataframe-in-new-tab và http://stackoverflow.com/ câu hỏi/37439014/có thể-cho-pandas-dataframe-to-be-rendered-in-a-new-cửa sổ Hoặc hiển thị dữ liệu trong cửa sổ khác: Excel, PyQt hoặc tkinter http://stackoverflow.com/questions/10636024/python-pandas-gui-cho-xem-a-dataframe-hoặc-ma trận –