简体   繁体   English

熊猫数据框中行中的唯一文本

[英]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:我有一个包含文本和数字数据的文本文件,格式如下图所示:

在此处输入图片说明

I am importing this file using pandas using the following command:我使用以下命令使用pandas导入此文件:

 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.在这个文件中,我想在名为Tag ( ['Tag1', 'Tag1', 'Tag1', Tag1, 'Tag5'] ) 的行中找到作为 python 列表的唯一文本。 How can I do it?我该怎么做?

When I use df.columns , I get this:当我使用df.columns ,我得到了这个:

>>> 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 ?在上述示例中,如何获取名为Tag的行中的唯一文本? Thanks.谢谢。

tolistlevels

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM