lse*_*ohn 124 python datetime dataframe pandas
我想打印整个数据帧,但我不想打印索引
另外,一列是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
Run Code Online (Sandbox Code Playgroud)
我希望它打印为
User ID Enter Time Activity Number
123 00:09:00 1411
123 00:18:00 893
123 00:49:00 1041
Run Code Online (Sandbox Code Playgroud)
小智 166
print df.to_string(index=False)
Run Code Online (Sandbox Code Playgroud)
Ant*_*ins 27
保留“漂亮打印”使用
from IPython.display import HTML
HTML(df.to_html(index=False))
Run Code Online (Sandbox Code Playgroud)
U2E*_*EF1 26
print(df.to_csv(sep='\t', index=False))
Run Code Online (Sandbox Code Playgroud)
或者可能:
print(df.to_csv(columns=['A', 'B', 'C'], sep='\t', index=False))
Run Code Online (Sandbox Code Playgroud)
kin*_*ing 12
如果你想漂亮地打印数据框,那么你可以使用tabulate包。
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)
Run Code Online (Sandbox Code Playgroud)
具体来说,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 |
+--------+--------+--------+
Run Code Online (Sandbox Code Playgroud)
要回答“如何在没有索引的情况下打印数据帧”问题,您可以将索引设置为空字符串数组(数据帧中的每一行一个),如下所示:
blankIndex=[''] * len(df)
df.index=blankIndex
Run Code Online (Sandbox Code Playgroud)
如果我们使用您帖子中的数据:
row1 = (123, '2014-07-08 00:09:00', 1411)
row2 = (123, '2014-07-08 00:49:00', 1041)
row3 = (123, '2014-07-08 00:09:00', 1411)
data = [row1, row2, row3]
#set up dataframe
df = pd.DataFrame(data, columns=('User ID', 'Enter Time', 'Activity Number'))
print(df)
Run Code Online (Sandbox Code Playgroud)
通常会打印为:
User ID Enter Time Activity Number
0 123 2014-07-08 00:09:00 1411
1 123 2014-07-08 00:49:00 1041
2 123 2014-07-08 00:09:00 1411
Run Code Online (Sandbox Code Playgroud)
通过创建一个包含与数据框中行数一样多的空字符串的数组:
blankIndex=[''] * len(df)
df.index=blankIndex
print(df)
Run Code Online (Sandbox Code Playgroud)
它将从输出中删除索引:
User ID Enter Time Activity Number
123 2014-07-08 00:09:00 1411
123 2014-07-08 00:49:00 1041
123 2014-07-08 00:09:00 1411
Run Code Online (Sandbox Code Playgroud)
在 Jupyter Notebooks 中将按照此屏幕截图呈现: Juptyer Notebooks dataframe with no index column
The line below would hide the index column of DataFrame when you print
df.style.hide_index()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
136647 次 |
| 最近记录: |