gpt4 book ai didi

python - 将 2 位发言者的对话从时间戳分组到数据帧 : Pandas

转载 作者:行者123 更新时间:2023-12-03 06:42:20 25 4
gpt4 key购买 nike

我有两个发言者“A”和“B”之间的对话及其时间戳。连续的 A 直到 B 提出问题,后面的 B 直到 A 给出问题的答案。我想制作一个问答对,其中 A 提出问题,B 回答(这将是数据框的一行)

注意:每个时间戳都会说出一个单词数据集如下所示:

<表类=“s-表”><标题>索引字偏移量发言者 <正文>85该433900000一个86客户端439300000一个87要求443600000一个88的449800000一个89正在迁移451400000一个90到456800000一个91首页457400000B92该462800000一个93雪花463700000一个94业力509800000B

我刚刚开始学习python,所以请不要太严厉。这是我的代码:

filename="https://docs.google.com/spreadsheets/d/1Sn1RHvhdHIrCuO_Yebohyk17Ow0IkBej/edit?usp=sharing&ouid=105987366122617111891&rtpof=true&sd=true"

`import pandas as pd
df2=pd.read_excel(filename="")
df3=pd.DataFrame(index=np.arange(100), columns=["Question","Answer"])
a=[]
j=0
for i in range(len(df2)):
while(df2["Speaker"][i]=="A"):
a.append(df2["Word"][i])
df3["Question"][j]=a
a.clear()
else:
a.append(df2["Word"][i])
df3["Answer"][j]=a
a.clear()
j=j+1`

最佳答案

在您简单地标记与不同说话者相关的每条线索后,不进行排序的 groupby 操作将提供结果。

import pandas as pd
import itertools
df = pd.read_excel('dataset.xlsx')
string_groups = sum([['%s_%s' % (i,n) for i in g] for n,(k,g) in enumerate(itertools.groupby(df.Speaker))],[])
df['Sector'] = string_groups
print(df.groupby('Sector', sort=False).agg({'Word': ' '.join}))

输出:

                                                     Word
Sector
A_0 the problem customer problem why are they migr...
B_1 yeah
A_2 so what exactly is the client requirement of m...
B_3 home
A_4 the snowflake
B_5 karma city park now it is

关于python - 将 2 位发言者的对话从时间戳分组到数据帧 : Pandas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74419767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com