垂直打印字符串列表

时间:2018-03-04 19:21:59

标签: python numpy

我有一个数据框如下。

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

3 个答案:

答案 0 :(得分: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))

答案 1 :(得分:2)

@membraneporential略有改动。

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

答案 2 :(得分:0)

你有正确的想法,只需将值与常规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