- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含许多条目的 DF。 DF 的摘录如下所示。
DF_OLD =
...
sID tID NER token Prediction
274 79 U-Peop khrushchev Live_In-ARG2+B
274 79 O 's Live_IN-ARG2+L
807 53 U-Loc louisiana Live_IN-ARG2+U
807 56 B-Peop earl Live_IN-ARG1+B
807 57 L-Peop long Live_IN-ARG1+L
807 13 B-Peop dwight Live_IN-ARG1+B
807 13 I-Peop d. Live_IN-ARG1+I
807 13 L-Peop eisenhower Live_IN-ARG1+L
...
sID
列分隔不同的句子。 预测
列显示机器学习分类器的结果。这些可能是相当荒谬的。我的目标是将所有预测标签分组到如下方案中:
DF_Expected =
...
sID entity1 tID1 entity2 tID2 Relation
274 NaN NaN khrushchev 's 79 Live_In
807 earl long 56 57 louisiana 53 Live_In
807 dwight d. eisenhower 13 louisiana 53 Live_In
...
“-ARGX-”部分显示实体在表中的位置,而第一个“-”之前的部分显示关系。如果缺少参数部分之一,则相应的单元格应为空。
这是我尝试过的:
DF["Live_In_Predict_Split"] = DF["Prediction"].str.split("+").str[0]
DF["token2"] = DF["token"]
DF["tokenID2"] = DF["tokenID"]
DF["Live_In_Predict2"] = DF["Live_In_Predict"]
data_tokeni_map = DF.groupby(["Live_In_Predict_Split","sentenceID"],as_index=True, sort=False).agg(" ".join).reset_index()
s = data_tokeni_map.loc[:,['sentenceID','token2',"tokenID2","Live_In_Predict2"]].merge(data_tokeni_map.loc[:,['sentenceID','token',"tokenID","Live_In_Predict"]],on='sentenceID')
s = s.loc[s.token2!=s.token].drop_duplicates()
我缺少某种计数器来区分不同的“-ARGX-”和某种 GroupBy 函数(GroupingBy tokenID 并不智能,因为它会产生错误的结果)。因此我的新 DF 是错误的:
DF_EDITED =
...
sID entity1 tID1 entity2 tID2 ...
807 dwight d eisenhower earl long 13 56 57 louisiana 53
807 louisiana 13 56 57 dwight d eisenhower earl long 53
编辑:
稍微改变了我的代码。现在,所有无用的预测都被删除,但所有相似的预测都被分组在一起。我需要某种数据预处理算法来匹配这样形式的数据,这意味着我需要计算每个 sID
的所有预测并对它们进行排序。
DF_OLD_Edit =
...
sID tID NER token Prediction
274 79 U-Peop khrushchev Live_In-ARG2+B_1
274 79 O 's Live_IN-ARG2+L_1
807 53 U-Loc louisiana Live_IN-ARG2+U_1
807 56 B-Peop earl Live_IN-ARG1+B_1
807 57 L-Peop long Live_IN-ARG1+L_1
807 13 B-Peop dwight Live_IN-ARG1+B_2
807 13 I-Peop d. Live_IN-ARG1+I_2
807 13 L-Peop eisenhower Live_IN-ARG1+L_2
...
最佳答案
数据:
df
sID tID NER token Prediction
0 274 79 U-Peop khrushchev Live_IN-ARG2+B_1
1 274 79 O 's Live_IN-ARG2+L_1
2 807 53 U-Loc louisiana Live_IN-ARG2+U_1
3 807 56 B-Peop earl Live_IN-ARG1+B_1
4 807 57 L-Peop long Live_IN-ARG1+L_1
5 807 13 B-Peop dwight Live_IN-ARG1+B_2
6 807 13 I-Peop d. Live_IN-ARG1+I_2
7 807 13 L-Peop eisenhower Live_IN-ARG1+L_2
代码:
import numpy as np
import pandas as pd
import typing
# setting up some columns for groupby
df['arg'] = df.Prediction.apply(lambda x: x.split("_")[1].split("-")[1].split("+")[0])
df['Relation'] = df.Prediction.apply(lambda x: x.split("-")[0])
df['ingroup_id'] = df.Prediction.apply(lambda x: x.split("_")[-1])
# groupby and collect relevant tID and token
df1 = df.groupby(['sID', 'arg', 'ingroup_id']).tID.apply(list)
df2 = df.groupby(['sID', 'arg', 'ingroup_id']).token.apply(list)
df3 = pd.concat([df1, df2], axis=1).reset_index()
df3.tID = df3.tID.apply(lambda x: list(set(x)))
# setting up columns that we finally use
df3.loc[df3.arg == 'ARG1', 'tID1'] = df3.tID
df3.loc[df3.arg == 'ARG2', 'tID2'] = df3.tID
df3.loc[df3.arg == 'ARG1', 'entity1'] = df3.token
df3.loc[df3.arg == 'ARG2', 'entity2'] = df3.token
# sort values and then ffill/bfill within the group
df3 = df3.sort_values(['sID', 'arg']).reset_index(drop=True)
df3.tID1 = df3.groupby(['sID']).tID1.ffill()
df3.entity1 = df3.groupby(['sID']).entity1.ffill()
df3.tID2 = df3.groupby(['sID']).tID2.bfill()
df3.entity2 = df3.groupby(['sID']).entity2.bfill()
df3 = df3[['sID', 'entity1', 'tID1', 'entity2', 'tID2']].set_index('sID')
# converting lists in cells into strings (may be someone can make this as a one liner)
df3.entity1 = df3.entity1.apply(lambda x: ' '.join(x) if isinstance(x, typing.List) else np.nan)
df3.entity2 = df3.entity2.apply(lambda x: ' '.join(x) if isinstance(x, typing.List) else np.nan)
df3.tID1 = df3.tID1.apply(lambda x: ' '.join(str(y) for y in x) if isinstance(x, typing.List) else np.nan)
df3.tID2 = df3.tID2.apply(lambda x: ' '.join(str(y) for y in x) if isinstance(x, typing.List) else np.nan)
df3 = df3.drop_duplicates().reset_index()
df3 = df3.merge(df[['sID', 'Relation']].drop_duplicates(), on='sID', how='left')
输出:
sID entity1 tID1 entity2 tID2 Relation
0 274 NaN NaN khrushchev 's 79 Live_IN
1 807 earl long 56 57 louisiana 53 Live_IN
2 807 dwight d. eisenhower 13 louisiana 53 Live_IN
由于缺乏我的技能,代码很长,但基本上它的作用是 groupby
和 merge
正如您在标题中所建议的那样。希望这会有所帮助。
关于python - 使用 merge 和 groupby 将 DF 引入新方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51969506/
我应该编写一个函数来打印一组给定的三个数字中两个较大数字的平方和。 我对这种情况的处理相当笨拙。我没有编写返回一组 3 中最大的两个数字的函数,而是编写了函数,以便表达式减少到两个所需的数字。 # S
如果有人可以提供帮助,我将不胜感激。我一直在敲我的头一天试图让这个工作。我已经在互联网上搜索并重新阅读了手册,但我就是不明白。 guile << __EOF__ ( define heading-li
目前我正在处理一个方案问题,其中我们正在使用方案列表表示一个图。我们使用的第一个变体是表示为 的边列表图 '((x y) (y z) (x z)) 我们正在使用的图的第二个变体被称为 x 图,表示为
我正在尝试创建一个函数,该函数将两个函数作为参数并执行它们。 我尝试使用 cond ,但它只执行 action1 . (define seq-action (lambda (action1 act
我提前为我的原始英语道歉;我会尽量避免语法错误等。 两周前,我决定更新我对 Scheme(及其启示)的知识,同时实现我在手上获得的一些数学 Material ,特别是我注册的自动机理论和计算类(cla
Scheme中有没有函数支持分数的“div”操作? 意思是 - 11 格 2.75 = 4。 最佳答案 我认为你的问题的答案是:没有,但你可以定义它: #lang racket (define (di
我在scheme中实现合并排序,我必须通过定义两个辅助方法来实现:merge和split。 Merge 需要两个列表(已经按递增顺序)并将它们合并在一起。我这样做了如下: (define merge
尝试从终端加载方案文件。我创建了一个名为 test.scm 的文件,其中包含以下代码: (define (square x) (* x x)) (define (sum-of-squares x y)
我有以下代码: (define (howMany list) (if (null? list) 0 (+ 1 (howMany (cdr list))))) 如果我们执行以
我有点了解如何将基本函数(例如算术)转换为Scheme中的连续传递样式。 但如果函数涉及递归怎么办?例如, (define funname (lambda (arg0 arg1)
我正在尝试附加两个字符串列表 但我不知道如何在两个单词之间添加空格。 (define (string-concat lst1 lst2) (map string-append lst1
这个问题已经有答案了: How do I pass a list as a list of arguments in racket? (2 个回答) 已关闭 8 年前。 我有一个函数,它需要无限数量的
我对这段代码的工作方式感到困惑: (define m (list 1 2 3 '(5 8))) (let ((l (cdr m))) (set! l '(28 88))) ==>(1 2 3 (5 8
我正在为学校做一项计划作业,有一个问题涉及我们定义记录“类型”(作为列表实现)(代表音乐记录)。 我遇到的问题是我被要求创建一个过程来创建这些记录的列表,然后创建一个将记录添加到该列表的函数。这很简单
我有以下代码: (define (howMany list) (if (null? list) 0 (+ 1 (howMany (cdr list))))) 如果我们执行以
我正在尝试附加两个字符串列表 但我不知道如何在两个单词之间添加空格。 (define (string-concat lst1 lst2) (map string-append lst1
如何使用抽象列表函数(foldr、foldl、map 和 filter 编写函数),无需递归,消耗数字列表 (list a1 a2 a3 ...) 并产生交替和 a1 - a2 + a3 ...? 最
我试图找出在 Scheme 中发生的一些有趣的事情: (define last-pair (lambda (x) (if (null? (cdr x))
这个问题在这里已经有了答案: Count occurrence of element in a list in Scheme? (4 个答案) 关闭 8 年前。 我想实现一个函数来计算列表中元素出现
我正在尝试使用下面的代码获取方案中的导数。谁能告诉我哪里出错了?我已经尝试了一段时间了。 (define d3 (λ (e) (cond ((number? e) 0) ((e
我是一名优秀的程序员,十分优秀!