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

时间:2014-07-09 02:50:30

标签: python datetime pandas dataframe

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

此外,一列是日期时间类型,我只想打印时间,而不是日期。

数据框如下所示:

   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

11 个答案:

答案 0 :(得分:143)

print df.to_string(index=False)

答案 1 :(得分:23)

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

或者可能:

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

答案 2 :(得分:10)

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

df.style.hide_index()

答案 3 :(得分:4)

要保留“精美打印”使用

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

enter image description here

答案 4 :(得分:3)

如果你只想打印一个字符串/ json,可以用以下方法解决:

print(df.to_string(index=False))

如果您想要序列化数据或甚至发送到MongoDB,最好是这样做:

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

现在有6种方法来定位数据,在panda docs中查看更适合您的更多内容。

答案 5 :(得分:1)

要回答“如何在不使用索引的情况下打印数据框”的问题,可以将索引设置为空字符串数组(数据框的每一行一个),如下所示:

router.get('/login-status', (req, res, next) => {    
  if (req.isAuthenticated()) {
    res.status(200).json(req.user);
  } else {
    res.status(403).json({
      success: false,
      error: 'User not Authenticated',
      message: "Please return to the login in page and try again."
    })
  }
})

如果我们使用您帖子中的数据:

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

通常会打印为:

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)

通过创建一个空字符串与数据框中的行数一样多的数组:

   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

它将从输出中删除索引:

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

在Jupyter中,笔记本将按照以下屏幕截图进行渲染:     Juptyer Notebooks dataframe with no index column

答案 6 :(得分:0)

如果要漂亮地打印数据框,则可以使用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)

具体来说,顾名思义,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 |
+--------+--------+--------+

答案 7 :(得分:0)

类似于上面使用df.to_string(index = False)的许多答案,我经常发现有必要提取单个值列,在这种情况下,您可以使用以下命令使用.to_string指定单个列:< / p>

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

这提供了易于复制(且无索引)的输出,可用于粘贴到其他地方(Excel)。样本输出:

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

答案 8 :(得分:0)

如果在Jupyter笔记本环境中使用数据框,请查看DataFrame.style属性:https://pandas.pydata.org/docs/user_guide/style.html

要在显示数据框时隐藏索引,请执行以下操作:

df.style.hide_index()

答案 9 :(得分:0)

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

display(table.hide_index())

答案 10 :(得分:0)

取自造王者的回答:

Jupyter notebook 可以在将单元格更改为 markdown 时将 GFM Markdown 表格语法转换为表格。

因此,将 tablefmt 更改为 'github' 而不是 'psql' 并复制和粘贴。

fields @timestamp, @message 
| parse @message '[] * {"*"}' as field1, field2, field3
| limit 50

(Python 3) enter image description here