我正在尝试将DataFrame与Series相乘,其中DataFrame与Series具有完全相同的索引,尽管索引值重复。理想的结果是,DataFrame中的每一行都会根据索引的值乘以Series中的相应值。
将具有重复索引值的系列乘以运算非常合理:
import pandas as pd
weights = pd.Series([10, 100], [0, 1])
s = pd.Series([1, 2, 3, 4], [0, 0, 1, 1])
s.mul(weights)
产生预期结果:
0 10
0 20
1 300
1 400
将DataFrame与重复索引值相乘会产生 ValueError:无法从重复轴重新索引:
df = pd.DataFrame({'a': [1, 2, 3, 4], 'b': [-1, -2, -3, -4]}, [0, 0, 1, 1])
df.mul(weights, axis=0)
...
ValueError: cannot reindex from a duplicate axis
如何达到以下效果?
a b
0 10 -10
0 20 -20
1 300 -300
1 400 -400
修改
一种选择是首先重新引导权重系列:
df.mul(weights.reindex(df.index, method='ffill'), axis=0)
答案 0 :(得分:1)
如何使用array = [10, 20, 30, 40, 50]
length = len(array)
n = 0
while n < length:
m = n
if m < length:
first = array[n]
if m+1 < length:
second = array[m+1]
else:
second = 0
if m+2 < length:
third = array[m+2]
else:
third = 0
result = first + second + third
n = n+1
print result
方法逐列数据框?
apply