![](/img/trans.png)
[英]Using pandas dataframe, how to group by multiple columns and adding new column
[英]Using polyfit on pandas dataframe and then adding the results to new columns
我有一個這樣的數據框。 對於每個 Id,我有 (x1,x2), (y1,y2)。 我想將這些提供給 polyfit(),獲取斜率和 x 截距並將它們添加為新列。
Id x y
1 0.79978 0.018255
1 1.19983 0.020963
2 2.39998 0.029006
2 2.79995 0.033004
3 1.79965 0.021489
3 2.19969 0.024194
4 1.19981 0.019338
4 1.59981 0.022200
5 1.79971 0.025629
5 2.19974 0.028681
我真的需要幫助對正確的行進行分組並將它們提供給 polyfit。 我一直在努力解決這個問題。 任何幫助將是最受歡迎的。
您可以groupby
,每個組內應用的契合。 首先,設置索引以便以后可以避免合並。
import pandas as pd
import numpy as np
df = df.set_index('Id')
df['fit'] = df.groupby('Id').apply(lambda x: np.polyfit(x.x, x.y, 1))
df
現在是:
x y fit
Id
1 0.79978 0.018255 [0.0067691538557680215, 0.01284116612923385]
1 1.19983 0.020963 [0.0067691538557680215, 0.01284116612923385]
2 2.39998 0.029006 [0.00999574968122608, 0.005016400680051043]
2 2.79995 0.033004 [0.00999574968122608, 0.005016400680051043]
3 1.79965 0.021489 [0.006761823817618233, 0.009320083766623343]
3 2.19969 0.024194 [0.006761823817618233, 0.009320083766623343]
...
如果您想為每個部分分別設置單獨的列,您可以應用 pd.Series。
df[['slope', 'intercept']] = df.fit.apply(pd.Series)
df = df.drop(columns='fit')
或者從最初的 DataFrame 堅持一個apply
並連接結果。
# From initial DataFrame
df = df.set_index('Id')
res = df.groupby('Id').apply(lambda x: pd.Series(np.polyfit(x.x, x.y, 1),
index=['slope', 'intercept']))
df = pd.concat([df, res], axis=1)
df
現在是:
x y slope intercept
Id
1 0.79978 0.018255 0.006769 0.012841
1 1.19983 0.020963 0.006769 0.012841
2 2.39998 0.029006 0.009996 0.005016
2 2.79995 0.033004 0.009996 0.005016
3 1.79965 0.021489 0.006762 0.009320
3 2.19969 0.024194 0.006762 0.009320
4 1.19981 0.019338 0.007155 0.010753
4 1.59981 0.022200 0.007155 0.010753
5 1.79971 0.025629 0.007629 0.011898
5 2.19974 0.028681 0.007629 0.011898
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.