【问题标题】:Printing a pandas dataframe without row number/index [duplicate]打印没有行号/索引的熊猫数据框[重复]
【发布时间】:2019-02-23 01:47:21
【问题描述】:

使用以下代码:

predictions  = pd.DataFrame([x6,x5,x4,x3,x2,x1])
print(predictions)

在控制台中打印以下内容:

            0
0  782.367392
1  783.314159
2  726.904744
3  772.089101
4  728.797342
5  753.678877

如何打印没有行 (0-5) 或列 (0) 索引的 predictions?

在 R 中,代码如下所示:

print(predictions, row.names = FALSE)

【问题讨论】:

  • 你可以简单地使用print(predictions.to_string(index=False))
  • @student 有正确答案
  • 你成功了@student
  • 而且我认为 Python 语法比 R 简单 :)
  • 很好用!我没有添加答案,因为它已经回答了。 Happy Coding。 :)

标签: python pandas


【解决方案1】:
print(df.to_string(index=False, header=False))

【讨论】:

  • 看起来不错。您也可以在打印之前进行一些舍入,例如df.round(2).to_string(...)
【解决方案2】:
print(predictions.values.tolist())

或者

df = pd.DataFrame(np.random.randn(5, 3), columns=list('ABC'))

>>> for row in df.values: print(row)
[-1.09989127 -0.17242821 -0.87785842]
[ 0.04221375  0.58281521 -1.10061918]
[ 1.14472371  0.90159072  0.50249434]
[ 0.90085595 -0.68372786 -0.12289023]
[-0.93576943 -0.26788808  0.53035547]

【讨论】:

【解决方案3】:

或使用:

predictions[0].tolist()

或者可以这样做:

'\n'.join(map(str,predictions[0].tolist()))

或者可以这样做:

for i in str(df).splitlines()[1:]:
    print(i.split(None,1)[1])

或者想赋值给字符串:

s=''
for i in str(df).splitlines()[1:]:
    s+=i.split(None,1)[1]+'\n'
print(s.rstrip())

【讨论】:

  • 这仍然有逗号,无论如何只是简单地打印数据框中的数字?
  • @ZJAY 那么,'\n'.join(map(str,predictions[0].tolist()))
猜你喜欢
  • 2014-08-29
  • 2018-01-15
  • 2023-01-26
  • 2013-11-05
  • 2020-03-19
  • 2015-04-19
  • 2021-02-26
相关资源
最近更新 更多