ben121
ben121

Reputation: 897

Print string list vertically

I have a dataframe as below.

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

I want to return a text string only including those which are more than 10.

example of the result i want is below

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

I have tried the below, but it didn't give the output that i was looking for. It gave an array in the same line. Not as the above.

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

Upvotes: 1

Views: 2387

Answers (3)

Mike Peder
Mike Peder

Reputation: 738

Slight alteration from @membraneporential.

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

Upvotes: 2

membranepotential
membranepotential

Reputation: 141

You had the right idea, just join the values together with regular 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))

Out:

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

Upvotes: 0

wohe1
wohe1

Reputation: 775

change

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

to

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

Upvotes: 2

Related Questions