4 回答

TA貢獻(xiàn)1813條經(jīng)驗(yàn) 獲得超2個(gè)贊
要獲得格式良好(灰色和白色的表格格式)數(shù)據(jù)框,您可以將其留在單元格的末尾,但這僅適用于單個(gè)數(shù)據(jù)框。如果想要打印多個(gè)格式良好的數(shù)據(jù)幀,可以使用
1)
from IPython.display import display
display(df1) #displays nicely formatted dataframe1
display(df2) #displays nicely formatted dataframe2
或者
2)
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
df1
df2
#displays both dataframes, nicely formatted
注意:漂亮的格式是 Jupyter notebook 呈現(xiàn)(樣式化和顯示)數(shù)據(jù)框的 HTML 版本,與瀏覽器呈現(xiàn) html 代碼的方式相同。(這里雖然從表的df1.to_html()獲得的真實(shí) HTML 代碼更少由瀏覽器呈現(xiàn)時(shí)作為獨(dú)立 html 代碼的樣式,因?yàn)?Jupyter Notebook 添加了一些額外的樣式)

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊
為什么 jupyter 有時(shí)打印格式化的 DataFrame 有時(shí)打印為文本?
如果您使用該函數(shù) -print
它會(huì)將其打印為文本,因?yàn)?print 正在為它獲取的任何對(duì)象使用函數(shù) to_string 。
當(dāng)您將數(shù)據(jù)框“留在”單元格的和處時(shí),它會(huì)將其顯示為表格,因?yàn)樗?Jupiter 的功能之一。
該函數(shù)test_csv.drop(columns=['platform'])
返回 df 如果你想讓它做你必須使用的數(shù)據(jù)幀的下降,inplace=True
或者df=df.drop(col)...
打印數(shù)據(jù)幀

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
test_csv.drop(columns=['platform'])
實(shí)際上并沒有刪除該列。它只是通過打印其狀態(tài)向您顯示數(shù)據(jù)框的臨時(shí)圖片。
要實(shí)際刪除該列:
test_csv.drop(columns=['platform'], inplace=True)
或者
test_csv.drop('platform', axis=1, inplace=True)
test_csv['aggregator'] = None
通過為其分配新列來更改數(shù)據(jù)框的狀態(tài)。因此,它不打印任何東西。

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊
我認(rèn)為您在 Jupyter Notebook 的同一個(gè)單元格中運(yùn)行這兩個(gè)語句
您可以在一個(gè)單元格中運(yùn)行以下代碼片段
# (1) How to add a new column?
test_csv['aggregator'] = None
test_csv # no need to use print
在另一個(gè)牢房里
# (2) How to remove a column?
test_csv.drop(columns=['platform'])
添加回答
舉報(bào)