亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

大熊貓中棘手的級聯分組

大熊貓中棘手的級聯分組

汪汪一只貓 2023-07-11 18:15:25
我想在 pandas 中解決一個奇怪的問題。假設我有一堆對象,它們有不同的分組方式。這是我們的數據框的樣子:df=pd.DataFrame([    {'obj': 'Ball',    'group1_id': None, 'group2_id': '7' },    {'obj': 'Balloon', 'group1_id': '92', 'group2_id': '7' },    {'obj': 'Person',  'group1_id': '14', 'group2_id': '11'},    {'obj': 'Bottle',  'group1_id': '3',  'group2_id': '7' },    {'obj': 'Thought', 'group1_id': '3',  'group2_id': None},])obj       group1_id          group2_idBall      None               7Balloon   92                 7Person    14                 11Bottle    3                  7Thought   3                  None我想根據任何組將事物分組在一起。這里注釋一下:obj       group1_id          group2_id    # annotatedBall      None               7            #                   group2_id = 7Balloon   92                 7            # group1_id = 92 OR group2_id = 7Person    14                 11           # group1_id = 14 OR group2_id = 11Bottle    3                  7            # group1_id =  3 OR group2_id = 7Thought   3                  None         # group1_id = 3組合后,我們的輸出應如下所示:count         objs                               composite_id4             [Ball, Balloon, Bottle, Thought]   g1=3,92|g2=71             [Person]                           g1=11|g2=14請注意,我們可以獲得的前三個對象group2_id=7,然后是第四個對象Thought,是因為它可以通過group1_id=3為其分配group_id=7id 來與另一個項目匹配。注意:對于這個問題,假設一個項目只會屬于一個組合組(并且永遠不會有可能屬于兩個組的情況)。我怎樣才能做到這一點pandas?
查看完整描述

2 回答

?
郎朗坤

TA貢獻1921條經驗 獲得超9個贊

這一點也不奇怪~網絡問題


import networkx as nx

#we need to handle the miss value first , we fill it with same row, so that we did not calssed them into wrong group

df['key1']=df['group1_id'].fillna(df['group2_id'])

df['key2']=df['group2_id'].fillna(df['group1_id'])

# here we start to create the network

G=nx.from_pandas_edgelist(df, 'key1', 'key2')

l=list(nx.connected_components(G))

L=[dict.fromkeys(y,x) for x, y in enumerate(l)]

d={k: v for d in L for k, v in d.items()}

# we using above dict to map the same group into the same one in order to groupby them?

out=df.groupby(df.key1.map(d)).agg(objs = ('obj',list) , Count = ('obj','count'), g1= ('group1_id', lambda x : set(x[x.notnull()].tolist())), g2= ('group2_id',? lambda x : set(x[x.notnull()].tolist())))

# notice here I did not conver the composite id into string format , I keep them into different columns which more easy to understand?

Out[53]:?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? objs? Count? ? ? ?g1? ? g2

key1? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

0? ? ?[Ball, Balloon, Bottle, Thought]? ? ? 4? {92, 3}? ?{7}

1? ? ? ? ? ? ? ? ? ? ? ? ? ? ?[Person]? ? ? 1? ? ?{14}? {11}


查看完整回答
反對 回復 2023-07-11
?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

這里有一個更詳細的解決方案,我為分組集合構建了“第一個鍵”的映射:


# using four id fields instead of 2

grouping_fields = ['group1_id', 'group2_id', 'group3_id', 'group4_id']

id_fields = df.loc[df[grouping_fields].notnull().any(axis=1), grouping_fields]


# build a set of all similarly-grouped items

# and use the 'first seen' as the grouping key for that

FIRST_SEEN_TO_ALL = defaultdict(set)

KEY_TO_FIRST_SEEN = {}


for row in id_fields.to_dict('records'):

? ? # why doesn't nan fall out in a boolean check?

? ? keys = [id for id in row.values() if id and (str(id) != 'nan')]

? ? row_id = keys[0]

? ? for key in keys:

? ? ? ? if (row_id != key) or (key not in KEY_TO_FIRST_SEEN):

? ? ? ? ? ? KEY_TO_FIRST_SEEN[key] = row_id

? ? ? ? ? ? first_seen_key = row_id

? ? ? ? else:

? ? ? ? ? ? first_seen_key = KEY_TO_FIRST_SEEN[key]

? ? ? ? FIRST_SEEN_TO_ALL[first_seen_key].add(key)


def fetch_group_id(row):

? ? keys = filter(None, row.to_dict().values())

? ? for key in keys:

? ? ? ? first_seen_key = KEY_TO_FIRST_SEEN.get(key)

? ? ? ? if first_seen_key:?

? ? ? ? ? ? return first_seen_key


df['group_super'] = df[grouping_fields].apply(fetch_group_id, axis=1)


查看完整回答
反對 回復 2023-07-11
  • 2 回答
  • 0 關注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號