2

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.

1 Answer 1

2

Just do levels with tolist

df.columns.levels[1].tolist()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Wen! This helped me very much. I will accept your answer in a few minutes. =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.