【问题标题】:Print plain output with only a space in python在python中打印只有一个空格的普通输出
【发布时间】:2018-11-14 19:43:09
【问题描述】:

我有一个包含以下列和索引的数据框:

   first  sec
0    z     50
2    b     3
1    a     3
4    c     2
5    e     1

我想打印这个简单的输出:

z 50
b 3
a 3
c 2
e 1

我使用了 to_string,但它没有给出想要的结果,我也使用了一些循环,但是没有给出想要的结果

【问题讨论】:

  • 列是否保证占用一个字符?
  • @WillemVanOnsem 是的,我刚刚更新了问题,第一列将始终占用一个字符,第二列可以包含多个字符,就像我在编辑中所做的那样

标签: python python-3.x python-2.7 pandas


【解决方案1】:

使用to_string 指定headerindexNone

>>> print(df.to_string(header=None, index=None))

z   50
b   3
a   3
c   2
e   1

如果您想要完全输出,您可以通过添加恰好一个空格来播放该系列,然后添加下一个系列。

>>> x = df['first'].add(' ').add(df['sec'].astype(str)).to_string(index=None).replace('\n ', '\n')

z 50
b 3
a 3
c 2
e 1

【讨论】:

  • 空格很多,我想要的正是我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 2013-11-06
  • 1970-01-01
  • 2015-09-29
  • 2012-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多