Python 3.x 框架复自连接
如果我有下面的框架Python 3.x 框架复自连接,python-3.x,pandas,Python 3.x,Pandas,如果我有下面的框架 id | value A | 50 B | 20 C | 10 A;B | C;B | A;B;C | 用70、30、80来填补空缺的最聪明的方法是什么 我试图将数据帧滑入两个dict和loop,但我觉得有更简单的方法 我的Python是3.5一种使用apply In [15]: mapper = df.dropna().set_index('id')['value'].to_dict() In [16]: df.id.apply
id | value
A | 50
B | 20
C | 10
A;B |
C;B |
A;B;C |
用70、30、80来填补空缺的最聪明的方法是什么
我试图将数据帧滑入两个dict和loop,但我觉得有更简单的方法
我的Python是3.5一种使用
apply
In [15]: mapper = df.dropna().set_index('id')['value'].to_dict()
In [16]: df.id.apply(lambda x: sum(map(mapper.get, x.split(';'))))
Out[16]:
0 50.0
1 20.0
2 10.0
3 70.0
4 30.0
5 80.0
Name: id, dtype: float64
使用
get_dummies
和dot
df.id.str.get_dummies(sep=';').dot([50,20,10])
Out[213]:
0 50
1 20
2 10
3 70
4 30
5 80
dtype: int64
整洁--df.id.str.get_dummies(“;”).dot(df.dropna().set_index('id'))