无法使用具有多索引的map函数将系列注入pandas数据帧

时间:2017-08-23 03:37:11

标签: python pandas

数据:

data = {"uid":{"0":"abc123","1":"abc123","2":"abc","3":"abc","4":"efgh"},"comp_id":{"0":1395,"1":2467,"2":4567,"3":1596,"4":111222},"retailer":{"0":"Shmo","1":"Joe","2":"Jon","3":"Sam","4":"Tim"},"price":{"0":7.49,"1":5.17,"2":89.99,"3":13.99,"4":4.98}}
stock = {"uid":{"0":"abc123","1":"abc123","2":"abc","3":"abc","4":"efgh"},"comp_id":{"0":1395,"1":2467,"2":4567,"3":1596,"4":111222},"availability":{"0":"True","1":"True","2":"True","3":"True","4":"True"}}
df = pd.DataFrame(data)
s = pd.DataFrame(stock)
s = s.set_index(['comp_id','uid'])

守则:

df.insert(2, 'availability', df.set_index(['uid','comp_id']).index.map(s['availability']))

错误:

TypeError: 'Series' object is not callable

另一种看法:

for idx in df.set_index(['uid','comp_id']).index:
       map(idx, s['availability'])

目标:

通常,我不得不从一个df注入一个系列到另一个df并且不想使用merge然后用列表推导重新排序col名称。我宁愿使用ordereddict操纵字典结构的数据帧或使用inject + map。如果你可以在首次创建数据帧时将它们组合起来,那也很酷。

1 个答案:

答案 0 :(得分:0)

试试这个:

In [174]: vals = df[['uid','comp_id']].set_index(['comp_id','uid']).join(s).values

In [175]: vals
Out[175]:
array([['True'],
       ['True'],
       ['True'],
       ['True'],
       ['True']], dtype=object)

In [176]: df.insert(2, 'availability', vals)

In [177]: df
Out[177]:
   comp_id  price availability retailer     uid
0     1395   7.49         True     Shmo  abc123
1     2467   5.17         True      Joe  abc123
2     4567  89.99         True      Jon     abc
3     1596  13.99         True      Sam     abc
4   111222   4.98         True      Tim    efgh