Pandas数据帧列标题到d的标签

2024-06-16 10:52:14 发布

您现在位置:Python中文网/ 问答频道 /正文

摘要:我的代码输出提供了一个以下格式的数据帧。dataframe的列标题是Content列中文本的标签。在下一步中,这些标签将作为多标签分类器的训练数据。这是一段更大的实际数据。在

因为它们是列标题,所以不可能将它们映射到作为其标签的文本。在

Content  A  B  C  D  E
    zxy  1  2     1   
    wvu  1     2  1   
    tsr  1  2        2
    qpo     1  1  1   
    nml        2  2   
    kji  1     1     2
    hgf        1     2
    edc  1  2     1              

更新:将df转换为csv显示空单元格为空(''vs' '): enter image description here

其中Content是文本所在的列,ABCD、和{}是需要转换为标签的列标题。只有带有1或2的列才是相关的。具有空单元格的列不相关,因此不需要转换为标签。在

更新:经过一番挖掘,也许数字不是整数,而是字符串。在

我知道在将文本+标签输入分类器进行处理时,两个数组的长度必须相等,否则不能将其视为有效输入。在

有没有一种方法可以将DF中Content中文本的列标题转换为标签?在

预期输出:

^{pr2}$

Tags: 数据代码文本标题dataframe分类器格式标签
3条回答

完整解决方案:

# first: clear all whitespace before and after a char, fine for all columns
for col in df.columns:
    df[col] = df[col].str.strip()

# fill na with 0
df.fillna(0, inplace=True)

# replace '' with 0
df.replace('', 0, inplace=True)

# convert to int, this must only be done on the specific columns with the numeric data
# this list is the column names as you've presented them, if they are different in the real data,
# replace them
for col in ['A', 'B', 'C', 'D', 'E']:
    df = df.astype({col: 'int16'})

print(df.info())

# you should end up with something like this.
"""
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 8 entries, 0 to 7
Data columns (total 6 columns):
Content    8 non-null object
A          8 non-null int16
B          8 non-null int16
C          8 non-null int16
D          8 non-null int16
E          8 non-null int16
dtypes: int16(5), object(1)
memory usage: 272.0+ bytes
"""

我们可以做^{},注意这里,我将空格视为np.nan,如果你的数据中是一个真正的空白,请更改最后一行

^{pr2}$

您也可以按如下方式进行操作:

# melt the two dimensional representation to
# a more or less onedimensional representation
df_flat= df.melt(id_vars=['Content'])
# filter out all rows which belong to empty cells
# the following is a fail-safe method, that should
# work for all datatypes you might encouter in your
# columns
df_flat= df_flat[~df_flat['value'].isna() & df_flat['value'] != 0]
df_flat= df_flat[~df_flat['value'].astype('str').str.strip().isin(['', 'nan'])]
# join the variables used per original row
df_flat.groupby(['Content']).agg({'variable': lambda ser: ', '.join(ser)})

输出如下:

^{pr2}$

给出以下输入数据:

import pandas as pd
import io

raw="""idx Content  A  B  C  D  E          
0   zxy      1  2     1                    
1   wvu      1     2  1                  
2   tsr      1  2        2               
3   qpo         1  1  1                  
4   nml            2  2                      
5   kji      1     1     2               
6   hgf            1     2               
7   edc      1  2     1           """

df= pd.read_fwf(io.StringIO(raw))
df.drop(['idx'], axis='columns', inplace=True)

编辑:我刚刚在阅读完之后删除了'idx',创建了一个类似于原始数据帧的结构,并添加了一些可以处理不同数据类型的故障保护代码(melt方法下面的两行)。如果对缺失值的实际表示方式了解得更多,代码就可以简化。在

下面是另一种使用np.wheregroupby的方法:

r, c = np.where(df>0)

df['Labels'] = pd.Series(df.columns[c], index=df.index[r]).groupby(level=[0, 1]).agg(', '.join)

输出:

^{pr2}$

相关问题 更多 >