我想從包含嵌套串列串列作為值的現有列創建兩列。
由 3 家公司參與者及其角色組成的記錄行:
**row 1** [{'roles': [{'type': 'director'}, {'type': 'founder'}, {'type': 'owner'}, {'type': 'real_owner'}], 'life': {'name': 'Lichun Du'}}]
**row 2** [{'roles': [{'type': 'board'}], 'life': {'name': 'Erik M?lgaard'}}, {'roles': [{'type': 'director'}, {'type': 'board'}, {'type': 'real_owner'}], 'life': {'name': 'Mikael Bodholdt Linde'}}, {'roles': [{'type': 'board'}, {'type': 'real_owner'}], 'life': {'name': 'Dorte B?cker Linde'}}]
**row 3** [{'roles': [{'type': 'director'}, {'type': 'real_owner'}], 'life': {'name': 'Kristian L?th Hougaard'}}, {'roles': [{'type': 'owner'}], 'life': {'name': 'WORLD JET HOLDING ApS'}}]
到目前為止,我已經嘗試過:
responses['Role of Participant(s)'] = [element[0]['roles'] for element in responses['participants']]
responses['Role of Participant(s)'] = responses['Role of Participant(s)'].apply(lambda x: ', '.join(t['type'] for t in x))
responses['Name of Participant(s)'] = [element[0]['life']['name'] for element in responses['participants']]
這給了我以下輸出:

它只回傳第一個參與者的角色和姓名
但是,我需要每行/記錄的所有參與者及其各自的角色,如下所示:

那么如何使用“***”作為每個行值的分隔符來實作這一點,就像上面的截圖一樣? 請幫忙!!
更新: 這是資料框的 csv 版本:
participants
"[{'roles': [{'type': 'founder'}], 'life': {'name': 'Poul Erik Andersen'}}, {'roles': [{'type': 'director'}, {'type': 'board'}], 'life': {'name': 'Martin Ravn-Nielsen'}}, {'roles': [{'type': 'board'}], 'life': {'name': 'S?ren Haugaard'}}, {'roles': [{'type': 'board'}], 'life': {'name': 'Mads Dehlsen Winther'}}, {'roles': [{'type': 'founder'}], 'life': {'name': 'M Ejendomme A/S'}}, {'roles': [{'type': 'founder'}], 'life': {'name': 'MILTON HOLDING HORSENS A/S'}}, {'roles': [{'type': 'accountant'}], 'life': {'name': 'EY Godkendt Revisionspartnerselskab'}}, {'roles': [{'type': 'owner'}], 'life': {'name': 'HUSCOMPAGNIET HOLDING A/S'}}]"
"[{'roles': [{'type': 'founder'}, {'type': 'director'}, {'type': 'board'}, {'type': 'real_owner'}], 'life': {'name': 'Rasmus Gert Hansen'}}, {'roles': [{'type': 'board'}], 'life': {'name': 'John Nyrup Larsen'}}, {'roles': [{'type': 'board'}], 'life': {'name': 'Ole Nidolf Larsen'}}, {'roles': [{'type': 'owner'}], 'life': {'name': 'RASMUS HANSEN HOLDING ApS'}}, {'roles': [{'type': 'accountant'}], 'life': {'name': 'DANSK REVISION SLAGELSE GODKENDT REVISIONSAKTIESELSKAB'}}]"
"[{'roles': [{'type': 'board'}], 'life': {'name': 'Berit Pedersen'}}, {'roles': [{'type': 'board'}], 'life': {'name': 'Sanne Kristine Sp?th'}}, {'roles': [{'type': 'real_owner'}], 'life': {'name': 'Kjeld Kirk Kristiansen'}}, {'roles': [{'type': 'director'}], 'life': {'name': 'Jesper Andersen'}}, {'roles': [{'type': 'board'}], 'life': {'name': 'Poul Hartvig Nielsen'}}, {'roles': [{'type': 'board'}], 'life': {'name': 'Nanna Birgitta Gudum'}}, {'roles': [{'type': 'board'}], 'life': {'name': 'Henrik Baag?e Fredel?kke'}}, {'roles': [{'type': 'board'}], 'life': {'name': 'Carsten Rasmussen'}}, {'roles': [{'type': 'board'}], 'life': {'name': 'Jesper Laursen'}}, {'roles': [{'type': 'board'}], 'life': {'name': 'John Hansen'}}, {'roles': [{'type': 'owner'}], 'life': {'name': 'LEGO A/S'}}, {'roles': [{'type': 'accountant'}], 'life': {'name': 'PRICEWATERHOUSECOOPERS STATSAUTORISERET REVISIONSPARTNERSELSKAB'}}]"
uj5u.com熱心網友回復:
你需要第二個for回圈而不是[0]
我使用普通函式而不是lambda使其更具可讀性。
首先是角色:
import pandas as pd
data = {'participants':
[
[{'roles': [{'type': 'director'}, {'type': 'founder'}, {'type': 'owner'}, {'type': 'real_owner'}], 'life': {'name': 'Lichun Du'}}],
[{'roles': [{'type': 'board'}], 'life': {'name': 'Erik M?lgaard'}}, {'roles': [{'type': 'director'}, {'type': 'board'}, {'type': 'real_owner'}], 'life': {'name': 'Mikael Bodholdt Linde'}}, {'roles': [{'type': 'board'}, {'type': 'real_owner'}], 'life': {'name': 'Dorte B?cker Linde'}}],
[{'roles': [{'type': 'director'}, {'type': 'real_owner'}], 'life': {'name': 'Kristian L?th Hougaard'}}, {'roles': [{'type': 'owner'}], 'life': {'name': 'WORLD JET HOLDING ApS'}}],
]
}
df = pd.DataFrame(data)
def get_roles(cell):
results = []
for item in cell:
roles = []
for role in item['roles']:
roles.append(role['type'])
results.append(",".join(roles))
results = "***".join(results)
return results
df['Role of Participant(s)'] = df['participants'].apply(get_roles)
print(df[['Role of Participant(s)']].to_string())
結果:
Role of Participant(s)
0 director,founder,owner,real_owner
1 board***director,board,real_owner***board,real_owner
2 director,real_owner***owner
現在你可以嘗試寫成 lambda
df['Role of Participant(s)'] = df['participants'].apply(lambda cell:"***".join([",".join(role['type'] for role in item['roles']) for item in cell]))
名稱相似:
def get_names(cell):
results = []
for item in cell:
results.append(item['life']['name'])
results = "***".join(results)
return results
df['Name of Participant(s)'] = df['participants'].apply(get_names)
現在有了 lambda
df['Name of Participant(s)'] = df['participants'].apply(lambda cell:"***".join(item['life']['name'] for item in cell))
編輯:
將兩列合二為一apply并跳過具有角色的參與者的版本director
import pandas as pd
data = {'participants':
[
[{'roles': [{'type': 'director'}, {'type': 'founder'}, {'type': 'owner'}, {'type': 'real_owner'}], 'life': {'name': 'Lichun Du'}}],
[{'roles': [{'type': 'board'}], 'life': {'name': 'Erik M?lgaard'}}, {'roles': [{'type': 'director'}, {'type': 'board'}, {'type': 'real_owner'}], 'life': {'name': 'Mikael Bodholdt Linde'}}, {'roles': [{'type': 'board'}, {'type': 'real_owner'}], 'life': {'name': 'Dorte B?cker Linde'}}],
[{'roles': [{'type': 'director'}, {'type': 'real_owner'}], 'life': {'name': 'Kristian L?th Hougaard'}}, {'roles': [{'type': 'owner'}], 'life': {'name': 'WORLD JET HOLDING ApS'}}],
]
}
df = pd.DataFrame(data)
def get_names_and_roles(cell):
all_names = []
all_roles = []
for item in cell:
name = item['life']['name']
roles = [role['type'] for role in item['roles']]
if 'director' not in roles:
all_names.append(name)
all_roles.append(",".join(roles))
all_names = "***".join(all_names)
all_roles = "***".join(all_roles)
return pd.Series([all_names, all_roles])
df[ ['Name of Participant(s)', 'Role of Participant(s)'] ] = df['participants'].apply(get_names_and_roles)
print(df[ ['Name of Participant(s)', 'Role of Participant(s)'] ].to_string())
結果:
Name of Participant(s) Role of Participant(s)
0
1 Erik M?lgaard***Dorte B?cker Linde board***board,real_owner
2 WORLD JET HOLDING ApS owner
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/358224.html
標籤:Python 熊猫 数据框 pandas-groupby 嵌套列表
