如何将np.gradient应用于熊猫groupbyseries?

问题描述 投票:1回答:1

np.gradient必须有2个以上的单词,熊猫不能将np.gradient应用于groupbySeries

grad_df = df.groupby(group_id)['lat'].apply(lambda x: np.gradient(x)).reset_index()
grad_df['lon'] = df.groupby(group_id)['lon'].apply(lambda x: np.gradient(x)).reset_index()['lon']

将提高

ValueError: Shape of array too small to calculate a numerical gradient, at least (edge_order + 1) elements are required.
python pandas numpy feature-extraction
1个回答
0
投票

我知道我的问题

这是

df2 = pd.DataFrame(np.array([[1, 2, 3,4], [1, 5, 6,7], [7, 8, 9,10],[8,8,10,12]]),
                   columns=['a', 'b', 'c','d'])

df3 = pd.DataFrame(np.array([[1, 2, 3,4], [1, 5, 6,7], [7, 8, 9,10],[7,8,10,12]]),
                   columns=['a', 'b', 'c','d'])

我们将会得到enter image description here

enter image description here

如果我对df2进行分组

df2.groupby('a')['d'].apply(lambda x:np.gradient(x))

然后enter image description here

然后enter image description here

所以,groupSeries无法将np.gradient应用于单个数字,这是我的答案

© www.soinside.com 2019 - 2024. All rights reserved.