【问题标题】:Print string list vertically垂直打印字符串列表
【发布时间】:2018-03-05 03:30:35
【问题描述】:

我有一个如下的数据框。

df = pd.DataFrame({'Title': ['x','y','z','aa'], 'Result': [2, 5, 11, 16]})

我想返回一个只包含超过 10 个的文本字符串。

我想要的结果示例如下

From the results in df, the below returned greater than 10:
    z
    aa

我已经尝试了以下方法,但它没有给出我正在寻找的输出。它在同一行给出了一个数组。不像上面那样。

df2 = df[df['Result']>= 10]
df3 = df2['Title'].values

print ('From the results in df, the below returned greater than 10:\n\t%s' (df3))

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    与@membraneporential 略有不同。

    print ('From the results in df, the below returned greater than 10:\n\t', '\n\t'.join(df3))
    

    【讨论】:

    • 啊是的更简洁,不知道为什么我认为生成器表达式是必要的
    【解决方案2】:

    改变

    print ('From the results in df, the below returned greater than 10:\n\t%s' (df3))
    

    print ('From the results in df, the below returned greater than 10:')
    for n in df3:
        print('\t' + str(n))
    

    【讨论】:

      【解决方案3】:

      您的想法是对的,只需将值与常规 python 连接在一起:

      long_values = df.loc[df['Result'] >= 10, 'Title']
      print('From the results in df, the below returned greater than 10:')
      print('\n'.join('\t' + s for s in long_values))
      

      输出:

      From the results in df, the below returned greater than 10:
          z
          aa
      

      【讨论】:

        猜你喜欢
        • 2014-06-29
        • 2019-11-14
        • 1970-01-01
        • 2010-10-09
        • 1970-01-01
        • 2013-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多