pandas:当索引不唯一时使用 diff 和 groupby 的问题 [英] pandas: problem using diff with groupby when index is non-unique
本文介绍了pandas:当索引不唯一时使用 diff 和 groupby 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 pandas(版本 0.20.3) 我想将 diff()
方法与 groupby()
一起应用,但不是 DataFrame,结果是下划线".
I am using pandas (version 0.20.3) and I want to apply the diff()
method with groupby()
but instead of a DataFrame, the result is an "underscore".
代码如下:
import numpy as np
import pandas as pd
# creating the DataFrame
data = np.random.random(18).reshape(6,3)
indexes = ['B']*3 + ['A']*3
columns = ['x', 'y', 'z']
df = pd.DataFrame(data, index=indexes, columns=columns)
df.index.name = 'chain_id'
# Now I want to apply the diff method in function of the chain_id
df.groupby('chain_id').diff()
结果是一个下划线!
注意 df.loc['A'].diff()
和 df.loc['B'].diff()
确实返回了预期的结果,所以我不明白为什么它不能与 groupby()
一起使用.
Note that df.loc['A'].diff()
and df.loc['B'].diff()
do return the expected results so I don't understand why it wouldn't work with groupby()
.
推荐答案
IIUC,您的错误:无法从重复轴重新索引
df.reset_index().groupby('chain_id').diff().set_index(df.index)
Out[859]:
x y z
chain_id
B NaN NaN NaN
B -0.468771 0.192558 -0.443570
B 0.323697 0.288441 0.441060
A NaN NaN NaN
A -0.198785 0.056766 0.081513
A 0.138780 0.563841 0.635097
这篇关于pandas:当索引不唯一时使用 diff 和 groupby 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文