2 回答
TA贡献1853条经验 获得超18个赞
我相信你需要
def cust_func(row):
results1 = []
results2 = []
p = row['parent'].lower()
for i in row['children']:
t = i.lower()
results1.append((i, len(set(t + p))))
results2.append((i, sum(int(k==v) for k,v in zip(t, p))))
return pd.Series([results1, results2])
df = pd.DataFrame({"parent": ['Max', 'Sam', 'Larry'], "children": [['MAX', 'amx', 'akd'], ['Sam','sammy','samsam'], ['lar','lair','larrylamo']]})
df[['results1', 'results2']] = df.apply(cust_func, axis=1)
print(df[['results1', 'results2']])
输出:
results1 results2
0 [(MAX, 3), (amx, 3), (akd, 5)] [(MAX, 3), (amx, 1), (akd, 0)]
1 [(Sam, 3), (sammy, 4), (samsam, 3)] [(Sam, 3), (sammy, 3), (samsam, 3)]
2 [(lar, 4), (lair, 5), (larrylamo, 6)] [(lar, 3), (lair, 3), (larrylamo, 5)]
TA贡献2003条经验 获得超2个赞
没有循环数据帧。只是一个用于位置明智比较的功能
df = pd.DataFrame([{'parent': 'MAX', 'children': ['MAX', 'amx', 'akd']},
{'parent': 'Sam', 'children': ['Sam', 'sammy', 'samsam']},
{'parent': 'Larry', 'children': ['lar', 'lair', 'larrylamo']}])
def placewise(a1, a2):
s = 0
for i in range(min(len(a1), len(a2))):
if a1[i] == a2[i]: s += 1
return s
df = (df.explode("children")
.assign(
results1=lambda x: x.apply(lambda r: (r["children"],
len(np.unique(np.concatenate([list(r["parent"].upper()),
list(r["children"].upper())])))
), axis=1),
results2=lambda x: x.apply(lambda r: (r["children"], placewise(r["parent"].upper(),
r["children"].upper()
)
), axis=1),
)
.groupby("parent", as_index=False).agg({"children":lambda x: list(x),
"results1":lambda x: list(x),
"results2":lambda x: list(x)})
)
print(df.to_string(index=False))
输出
parent children results1 results2
Larry [lar, lair, larrylamo] [(lar, 4), (lair, 5), (larrylamo, 6)] [(lar, 3), (lair, 3), (larrylamo, 5)]
MAX [MAX, amx, akd] [(MAX, 3), (amx, 3), (akd, 5)] [(MAX, 3), (amx, 1), (akd, 0)]
Sam [Sam, sammy, samsam] [(Sam, 3), (sammy, 4), (samsam, 3)] [(Sam, 3), (sammy, 3), (samsam, 3)]
添加回答
举报