在DataFram中组合行

2024-09-21 05:22:25 发布

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

我有一个DF,它具有NER分类器的结果,例如:

df =

s        token        pred       tokenID
17     hakawati       B-Loc         3
17     theatre        L-Loc         3
17     jerusalem      U-Loc         7
56     university     B-Org         5
56     of             I-Org         5
56     texas          I-Org         5
56     here           L-Org         6
...
5402   dwight         B-Peop        1    
5402   d.             I-Peop        1
5402   eisenhower     L-Peop        1  

此数据帧中还有许多其他列不相关。现在我想根据它们的sentenceID(=s)和它们的预测标记对标记进行分组,以将它们组合成一个实体:

df2 =


s        token                        pred               
17     hakawati  theatre           Location
17     jerusalem                   Location
56     university of texas here    Organisation
...
5402   dwight d. eisenhower        People

通常我会这样做,通过简单地使用像 data_map = df.groupby(["s"],as_index=False, sort=False).agg(" ".join)并使用重命名函数。然而,由于数据包含不同类型的字符串(B,I,L-Loc/Org..),我不知道如何准确地执行它

任何想法都很感激

有什么想法吗


Tags: oforgtokendfherelocpreduniversity
2条回答

您可以同时按stokenID分组,并按如下方式聚合:

def aggregate(df):
    token = " ".join(df.token)
    pred = df.iloc[0].pred.split("-", 1)[1]
    return pd.Series({"token": token, "pred": pred})

df.groupby(["s", "tokenID"]).apply(aggregate)

# Output
                             token  pred
s    tokenID                            
17   3            hakawati theatre   Loc
     7                   jerusalem   Loc
56   5         university of texas   Org
     6                        here   Org
5402 1        dwight d. eisenhower  Peop

一个解决方案通过一个辅助列

df['pred_cat'] = df['pred'].str.split('-').str[-1]

res = df.groupby(['s', 'pred_cat'])['token']\
        .apply(' '.join).reset_index()

print(res)

      s pred_cat                       token
0    17      Loc  hakawati theatre jerusalem
1    56      Org    university of texas here
2  5402     Peop        dwight d. eisenhower

请注意,这与所需的输出不完全匹配;似乎涉及到一些特定于数据的处理方法

相关问题 更多 >

    热门问题