[英]Convert elements of list in pandas series using a dict
我有以下Pandas數據幀:
1 ["Apple", "Banana"]
2 ["Kiwi"]
3 None
4 ["Apple"]
5 ["Banana", "Kiwi"]
和以下詞典:
{1: ["Apple", "Banana"],
2: ["Kiwi"]}
我現在想使用字典映射數據框中列表中的所有條目。 結果應如下:
1 [1]
2 [2]
3 None
4 [1]
5 [1, 2]
如何最有效地完成這項工作?
方法1我正在使用unnesting
d={z : x for x , y in d.items() for z in y }
s=unnesting(s.to_frame().dropna(),[0])[0]\
.map(d).groupby(level=0).apply(set).reindex(s.index)
Out[260]:
0 {1}
1 {2}
2 NaN
3 {1}
4 {1, 2}
Name: 0, dtype: object
方法2循環它
[set(d.get(y) for y in x) if x is not None else None for x in s ]
#s=[set(d.get(y) for y in x) if x is not None else None for x in s ]
Out[265]: [{1}, {2}, None, {1}, {1, 2}]
數據輸入
s=pd.Series([["Apple", "Banana"],["Kiwi"],None,["Apple"],["Banana", "Kiwi"]])
d={1: ["Apple", "Banana"],
2: ["Kiwi"]}
一種方法是首先取消字典並將值設置為鍵,並將其對應的鍵作為值。 然后,您可以使用列表推導並映射數據框中每個列表中的值。
在每次迭代中從映射返回結果之前,必須先取一set
,以避免重復的值。 另請注意, if x is not None else None
or None
則執行相同的操作if x is not None else None
此處,如果列表為空,則返回None
。 有關此問題的更詳細說明,您可以查看此帖子 :
df = pd.DataFrame({'col1':[["Apple", "Banana"], ["Kiwi"], None, ["Apple"], ["Banana", "Kiwi"]]})
d = {1: ["Apple", "Banana"], 2: ["Kiwi"]}
d = {i:k for k, v in d.items() for i in v}
# {'Apple': 1, 'Banana': 1, 'Kiwi': 2}
out = [list(set(d[j] for j in i)) or None for i in df.col1.fillna('')]
# [[1], [2], None, [1], [1, 2]]
pd.DataFrame([out]).T
0
0 [1]
1 [2]
2 None
3 [1]
4 [1, 2]
重建字典
m = {v: k for k, V in d.items() for v in V}
重建
x = s.dropna()
v = [*map(m.get, np.concatenate(x.to_numpy()))]
i = x.index.repeat(x.str.len())
y = pd.Series(v, i)
y.groupby(level=0).unique().reindex(s.index)
0 [1]
1 [2]
2 NaN
3 [1]
4 [1, 2]
dtype: object
如果你堅持使用None
而不是NaN
y.groupby(level=0).unique().reindex(s.index).mask(pd.isna, None)
0 [1]
1 [2]
2 None
3 [1]
4 [1, 2]
dtype: object
s = pd.Series([
['Apple', 'Banana'],
['Kiwi'],
None,
['Apple'],
['Banana', 'Kiwi']
])
d = {1: ['Apple', 'Banana'], 2: ['Kiwi']}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.