编辑:添加没有匹配索引的行以演示预期行为
我有以下两个 DataFrame:
请求
:
requests
asn pop country
1 1 us 100
br 50
2 br 200
3 hk 150
4 uk 100
2 1 us 300
...
流量
:
total capacity
asn pop
1 1 53 1000
2 15 1000
3 103 10000
2 1 254 10000
...
我希望向 requests
DataFrame 添加一个新列,其值等于 traffic["total"]/Traffic["capacity"]
,并与两个匹配的索引。
我尝试了以下方法:
>>>requests["network"] = traffic["total"] / traffic["capacity"]
>>>requests
requests network
asn pop country
1 1 us 100 NaN
br 50 NaN
2 br 200 NaN
3 hk 150 NaN
4 uk 100 NaN
2 1 us 300 NaN
...
当所有三个索引都可用时,这以前对我有用。然而在这个例子中我只有两个索引,所以它似乎失败了。
预期输出
>>>requests
requests network
asn pop country
1 1 us 100 0.053
br 50 0.053
2 br 200 0.015
3 hk 150 0.0103
4 uk 100 NaN
2 1 us 300 0.0254
...
最佳答案
您的MultiIndex
存在不匹配的问题,因此获取NaN
。解决方案是添加 reindex
.
requests['network'] = traffic["total"].div(traffic["capacity"])
.reindex(requests.index, method='ffill')
print (requests)
requests network
asn pop country
1 1 us 100 0.0530
br 50 0.0530
2 br 200 0.0150
3 hk 150 0.0103
2 1 us 300 0.0254
使用 reset_index
+ set_index
的旧解决方案:
requests = requests.reset_index(level=2)
requests['network'] = traffic["total"].div(traffic["capacity"])
requests = requests.set_index('country', append=True)
print (requests)
requests network
asn pop country
1 1 us 100 0.0530
br 50 0.0530
2 br 200 0.0150
3 hk 150 0.0103
2 1 us 300 0.0254
关于python - Pandas:通过对所有匹配索引进行算术添加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46202315/