In [66]: t1
Out[69]:
job_date branch_id
2018-05 1 0.618980
2 0.600590
3 0.603486
4 0.043931
5 0.588168
6 0.381518
7 0.357035
2018-06 1 0.690575
2 0.700900
3 0.571556
4 0.351935
5 0.626428
6 0.461813
7 0.329663
Name: utilization, dtype: float64
In [86]: t1.index
Out[86]:
MultiIndex(levels=[[2018-05, 2018-06], [1, 2, 3, 4, 5, 6, 7]],
labels=[[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1], [0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6]],
names=['job_date', 'branch_id'])
如何通过索引值对行进行区分?
所以差异
(2018-05,1)和(2018-06,1)应该是
0.690575-0.618980=0.071595
如果我执行t1.diff(),则会得到逐行比较,这不是我想要的
In [87]: t1.diff()
Out[87]:
job_date branch_id
2018-05 1 NaN
2 -0.018390
3 0.002895
4 -0.559554
5 0.544237
6 -0.206651
7 -0.024483
2018-06 1 0.333540
2 0.010325
3 -0.129345
4 -0.219621
5 0.274494
6 -0.164615
7 -0.132150
现在我正在做这个
In [49]: t1.unstack(level=0)['utilization'].diff(axis=1)
Out[49]:
job_date 2018-05 2018-06
branch_id
1 NaN 0.071595
2 NaN 0.100310
3 NaN -0.031930
4 NaN 0.308003
5 NaN 0.038260
6 NaN 0.080295
7 NaN -0.027372
有没有不堆叠的方法?
最佳答案
一种可能的解决方案是将MultiIndex
移位一个月并减去,如果每个Period
之间的差异相同,则可以正常工作-这里是一个月:
a = df.index.get_level_values(0).to_period('M')
b = df.index.get_level_values(1)
mux1 = pd.MultiIndex.from_arrays([a,b], names=df.index.names)
mux2 = pd.MultiIndex.from_arrays([a + 1, b], names=df.index.names)
df = df.set_index(mux1)
df1 = df.set_index(mux2)
df['utilization'] = df.sub(df1)
print (df)
utilization
job_date branch_id
2018-05 1 NaN
2 NaN
3 NaN
4 NaN
5 NaN
6 NaN
7 NaN
2018-06 1 0.071595
2 0.100310
3 -0.031930
4 0.308004
5 0.038260
6 0.080295
7 -0.027372
关于python - pandas .diff通过行的多索引值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53149855/