Fat Wallets
Fat Wallets

Reputation: 101

Pandas print unique values as string

I've got a list of unique value from selected column in pandas dataframe. What I want to achieve is to print the result as string.

import pandas as pd

df = pd.DataFrame({'A':['A','C','C','B','A','C','B']})
a = df['A'].unique()

print(a)

Output: ['A' 'C' 'B']

Desired output: A, C, B

So far I've tried below,

print(a.to_string())
Got this error: AttributeError: 'numpy.ndarray' object has no attribute 'to_string'

print(a.tostring())
Got this: b'\xf0\x04\xa6P\x9e\x01\x00\x000\xaf\x92P\x9e\x01\x00\x00\xb0\xaf\x92P\x9e\x01\x00\x00'

Can anyone give a hint.

Upvotes: 1

Views: 3493

Answers (3)

Ezer K
Ezer K

Reputation: 3739

py3 solution

df = pd.DataFrame({'A':['A','C','C','B','A','C','B']})
a = df['A'].unique()

print(*a,  sep=", ")

Upvotes: 2

Andrej Kesely
Andrej Kesely

Reputation: 195478

import pandas as pd

df = pd.DataFrame({'A':['A','C','C','B','A','C','B']})
a = df['A'].unique()

print(', '.join(a))  # or print(*a, sep=', ')

Prints:

A, C, B

EDIT: To store as variable:

text = ', '.join(a)
print(text)

Upvotes: 5

thorntonc
thorntonc

Reputation: 2126

This should work:

print(', '.join(a))

Upvotes: 2

Related Questions