简体   繁体   English

如何在没有索引的情况下打印 Pandas DataFrame

[英]How to print pandas DataFrame without index

I want to print the whole dataframe, but I don't want to print the index我想打印整个数据框,但我不想打印索引

Besides, one column is datetime type, I just want to print time, not date.此外,一列是日期时间类型,我只想打印时间,而不是日期。

The dataframe looks like:数据框看起来像:

   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

I want it print as我希望它打印为

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

python 2.7蟒蛇 2.7

print df.to_string(index=False)

python 3蟒蛇 3

print(df.to_string(index=False))

The line below would hide the index column of DataFrame when you print打印时,下面的行将隐藏 DataFrame 的索引列

df.style.hide_index()

Update: tested w Python 3.7更新:用 Python 3.7 测试

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

或者可能:

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

To retain "pretty-print" use保留“漂亮打印”使用

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

在此处输入图片说明

If you want to pretty print the data frames, then you can use tabulate package.如果你想漂亮地打印数据框,那么你可以使用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)

Specifically, the showindex=False , as the name says, allows you to not show index.具体来说, showindex=False ,顾名思义,允许您不显示索引。 The output would look as follows:输出将如下所示:

+--------+--------+--------+
|   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 |
+--------+--------+--------+

If you just want a string/json to print it can be solved with:如果您只想打印字符串/json,可以使用以下方法解决:

print(df.to_string(index=False))

Buf if you want to serialize the data too or even send to a MongoDB, would be better to do something like: Buf 如果您也想序列化数据甚至发送到 MongoDB,最好执行以下操作:

document = df.to_dict(orient='list')

There are 6 ways by now to orient the data, check more in the panda docs which better fits you.现在有 6 种方法可以定位数据,请在更适合您的panda 文档中查看更多信息。

To answer the "How to print dataframe without an index" question, you can set the index to be an array of empty strings (one for each row in the dataframe), like this:要回答“如何在没有索引的情况下打印数据帧”问题,您可以将索引设置为空字符串数组(数据帧中的每一行一个),如下所示:

blankIndex=[''] * len(df)
df.index=blankIndex

If we use the data from your post:如果我们使用您帖子中的数据:

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)

which would normally print out as:通常会打印为:

   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

By creating an array with as many empty strings as there are rows in the data frame:通过创建一个包含与数据框中行数一样多的空字符串的数组:

blankIndex=[''] * len(df)
df.index=blankIndex
print(df)

It will remove the index from the output:它将从输出中删除索引:

  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

And in Jupyter Notebooks would render as per this screenshot: Juptyer Notebooks dataframe with no index column在 Jupyter Notebooks 中将按照此屏幕截图呈现: Juptyer Notebooks dataframe with no index column

任何在 Jupyter Notebook 上工作以打印没有索引列的 DataFrame 的人,这对我有用:

display(table.hide_index())

Similar to many of the answers above that use df.to_string(index=False), I often find it necessary to extract a single column of values in which case you can specify an individual column with .to_string using the following:与上面使用 df.to_string(index=False) 的许多答案类似,我经常发现有必要提取一列值,在这种情况下,您可以使用以下内容使用 .to_string 指定单个列:

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

print(data.to_string(columns=['col1'], index=False)

print(data.to_string(columns=['col1', 'col2'], index=False))

Which provides an easy to copy (and index free) output for use pasting elsewhere (Excel).它提供了一个易于复制(和无索引)的输出,用于粘贴到其他地方(Excel)。 Sample output:示例输出:

col1  col2    
49    62    
97    97    
87    94    
85    61    
18    55

Taking from kingmakerking's answer:取自造王的回答:

Jupyter notebook can convert GFM Markdown table syntax into a table when you change the cell to markdown. Jupyter Notebook 可以在将单元格更改为 markdown 时将 GFM Markdown 表格语法转换为表格。

So, change tablefmt to 'github' instead of 'psql' and copy and paste.因此,将 tablefmt 更改为 'github' 而不是 'psql' 并复制和粘贴。

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

(Python 3) (蟒蛇 3) 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM