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

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

合并單元格而不丟失 Pandas 中的信息

合并單元格而不丟失 Pandas 中的信息

慕斯709654 2022-06-14 17:02:12
我正在編寫一個能夠從 Discogs 音樂數據庫中抓取專輯信息的程序。刮刀工作正?!,F在我有一個數據框,其中包含許多重復的藝術家和標題,其中格式單元格不同(例如,參見下面我的數據框片段中的“Sido”、“Ich und keine Maske”)。    Interpret                    Title                              Formats0   Afrika Bambaataa And Family  The Decade Of Darkness 1990-2000   CD, Album, RE1   Sha Hef                      Out The Mud2   Sido                         Ich Und Keine Maske                CD, Album3   Sido                         Ich Und Keine Maske                2xLP, Album...現在我正在尋找一種方法來組合這些雙重條目而不會丟失信息。有人可以給我一個提示嗎?最終結果應如下所示:    Interpret                    Title                              Formats0   Afrika Bambaataa And Family  The Decade Of Darkness 1990-2000   CD, Album, RE1   Sha Hef                      Out The Mud2   Sido                         Ich Und Keine Maske                CD, Album, 2xLP...我努力了r = dataframe.groupby('Interpret')['Formate'].apply(','.join)但結果是刪除了“標題”列的熊貓系列,所以我丟失了信息。
查看完整描述

1 回答

?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

你有沒有嘗試過


import pandas as pd

df = pd.DataFrame({'Interpret': ['Afrika Bambaataa And Family', 'Sha Hef', 'Sido', 'Sido'],

                   'Title': ['The Decade Of Darkness 1990-2000', 'Out The Mud', 'Ich Und Keine Maske', 'Ich Und Keine Maske'],                             

                   'Formats': ['CD, Album, RE', 'CD, Album', 'CD, Album', '2xLP, Album']})


# remove duplicate interprets and merge formats...

df1 = df.groupby('Interpret').agg(lambda x: ', '.join(x.unique())).reset_index()


# now to get rid of duplicate entries in 'Formats' column...

def drop_dupes(row):

    l = row.split(', ')

    return ', '.join(list(set(l)))


df1['Formats'] = df1['Formats'].apply(drop_dupes)

? 那給你


Out[40]: 

                     Interpret  ...          Formats

0  Afrika Bambaataa And Family  ...    CD, RE, Album

1                      Sha Hef  ...        CD, Album

2                         Sido  ...  CD, Album, 2xLP

并且基本上是您為這個問題找到的答案的略微修改版本。


查看完整回答
反對 回復 2022-06-14
  • 1 回答
  • 0 關注
  • 194 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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