三向對話:
red_01 回復 green_01。green_01 回復 red_01。這可以被認為是整個資料集中的 1 個三向對話。
所以,我試圖想出解決方案來查詢整個資料集中發生了多少這樣的對話?順便說一下,這是一對一的對話系統。
我已經按時間戳排序,對紅色用戶使用了 nth(0)、nth(1)。這僅保留了所有紅色用戶發送的前兩條訊息。我對所有綠色用戶都使用了 nth(0)。這保留了綠色用戶發送的所有第一個訊息。但是想不出任何東西來計算以三向對話的確切順序發生的對話數量(red_0x 發送訊息,green_0x 回復,red_0x 回復)
我有一個這樣的資料集:
| 對話ID | 用戶身份 | 訊息 | 時間戳 | |
|---|---|---|---|---|
| 1 | red_01 | |||
| 1 | 綠色_01 | |||
| 1 | red_01 |
uj5u.com熱心網友回復:
import pandas as pd
test_df = pd.DataFrame([[1,"red1","", 1],[1,"green1","", 2],[1,"red1","", 3], [2,"red1","", 1],[2,"red1","", 2],[2,"red1","", 3]],
columns = ["conversation_id","user_id","messages","timestamp"])
criteria = (
lambda x: len(x) == 3
and x.iloc[0].user_id.startswith("red")
and x.iloc[1].user_id.startswith("green")
and x.iloc[2].user_id.startswith("red")
)
n_groups = (
test_df
.sort_values("timestamp")
.groupby("conversation_id")
.filter(criteria)
).groupby("conversation_id").ngroups
n_groups
輸出:
1
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/525434.html
標籤:Python熊猫
上一篇:將txt檔案讀取為從串列中選擇的資料框時出錯-python/pandas
下一篇:在我的資料框中,我有時間列,我需要將我的HH:MM:SS.SS轉換為秒。我怎么能在python中做到這一點?[復制]
