Ling Guo
Ling Guo

Reputation: 592

Unique texts in row in pandas dataframe

I have a text file with text and numerical data in the format shown in the following picture:

enter image description here

I am importing this file using pandas using the following command:

 df = pd.read_csv('dum.txt',sep='\t', header=[0,1], index_col=0)

In this file, I want to find the unique texts in the row called Tag (['Tag1', 'Tag1', 'Tag1', Tag1, 'Tag5']) as a python list. How can I do it?

When I use df.columns, I get this:

>>> df.columns
MultiIndex(levels=[[u'T1', u'T2', u'T3', u'T4', u'T5'], 
   [u'Tag1', u'Tag5']], labels=[[0, 1, 2, 3, 4], [0, 0, 
   0, 0, 1]], names=[u'Type', u'Tag'])

In the aforesaid example, how can I get the unique texts in the row called Tag? Thanks.

Upvotes: 2

Views: 112

Answers (1)

BENY
BENY

Reputation: 323306

Just do levels with tolist

df.columns.levels[1].tolist()

Upvotes: 2

Related Questions