我有一個大約有 500 萬行的 Pandas 數據框,其中有 2 列“top_level_domain”和“category”。我想創建一個具有不同 top_level_domain 的新數據框和一個以逗號分隔的類別列以用于唯一類別。此數據框已具有按性質以逗號分隔的類別。其他域如 google 將具有重復類別,但我只想要一個。數據框:df1 top_level_domain category1 google.com Search Engines2 service-now.com Business, Software/Hardware3 google-analytics.com Internet Services4 live.com None Assigned5 google.com Content Server6 google.com Search Engines7 inspectlet.com Internet Services8 doubleclick.net Online Shopping, Web Ads9 google.com Search Engines10 doubleclick.net Ads期望的輸出:df2 top_level_domain category1 google.com Search Engines, Content Server2 service-now.com Business, Software/Hardware3 google-analytics.com Internet Services4 live.com None Assigned7 inspectlet.com Internet Services8 doubleclick.net Online Shopping, Web Ads, Ads實現這一目標的最佳方法是什么?我已經嘗試了Pandas groupby 多列、多列列表中的所有示例其他人喜歡下面的那個,但我仍然在類別列中收到重復項。distinct_category = distinct_category.groupby('top_level_domain')['category'].agg(lambda x: ', '.join(set(x))).reset_index()但我在列中得到重復1 zoho.com Online Shopping, Interactive Web Applications, Interactive Web Applications, Interactive Web Applications, Motor Vehicles1 zohopublic.com Internet Services, Motor Vehicles, Internet Services, Online Shopping, Internet Services
Pandas 按第一列分組并從第二列添加逗號分隔的條目
幕布斯6054654
2021-10-12 15:44:33