我想打印整个数据框架,但我不想打印索引

另外,其中一列是datetime类型,我只想打印时间,而不是日期。

数据框架看起来像这样:

   User ID           Enter Time   Activity Number
0      123  2014-07-08 00:09:00              1411
1      123  2014-07-08 00:18:00               893
2      123  2014-07-08 00:49:00              1041

我要按原样打印

User ID   Enter Time   Activity Number
123         00:09:00              1411
123         00:18:00               893
123         00:49:00              1041

当前回答

选自国王的回答:

当您将单元格更改为Markdown时,Jupyter笔记本可以将GFM Markdown表语法转换为表。

所以,改变tablefmt到'github'而不是'psql'和复制粘贴。

    print(tabulate(dframe, headers='keys', tablefmt='github', showindex=False))

(Python 3)

其他回答

选自国王的回答:

当您将单元格更改为Markdown时,Jupyter笔记本可以将GFM Markdown表语法转换为表。

所以,改变tablefmt到'github'而不是'psql'和复制粘贴。

    print(tabulate(dframe, headers='keys', tablefmt='github', showindex=False))

(Python 3)

print(df.to_csv(sep='\t', index=False))

或者是:

print(df.to_csv(columns=['A', 'B', 'C'], sep='\t', index=False))

打印时,下面的行将隐藏DataFrame的索引列

df.style.hide_index()

保持“漂亮的印刷”的使用

from IPython.display import HTML
HTML(df.to_html(index=False))

如果你想漂亮地打印数据帧,那么你可以使用表格包。

import pandas as pd
import numpy as np
from tabulate import tabulate

def pprint_df(dframe):
    print tabulate(dframe, headers='keys', tablefmt='psql', showindex=False)

df = pd.DataFrame({'col1': np.random.randint(0, 100, 10), 
    'col2': np.random.randint(50, 100, 10), 
    'col3': np.random.randint(10, 10000, 10)})

pprint_df(df)

具体来说,showindex=False,顾名思义,允许不显示索引。输出如下所示:

+--------+--------+--------+
|   col1 |   col2 |   col3 |
|--------+--------+--------|
|     15 |     76 |   5175 |
|     30 |     97 |   3331 |
|     34 |     56 |   3513 |
|     50 |     65 |    203 |
|     84 |     75 |   7559 |
|     41 |     82 |    939 |
|     78 |     59 |   4971 |
|     98 |     99 |    167 |
|     81 |     99 |   6527 |
|     17 |     94 |   4267 |
+--------+--------+--------+