5
私はグループ化したDataFrameを持っています。 データフレームに、グループごとに関数diffの結果である別の列を追加したいとします。私は、各グループごとに列Dのdifferneceを取得し、差分計算を使用して新しい列を含めるDFを持っていると思いパンダグループを変更する
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'foo'],
'B' : ['one', 'one', 'two', 'three',
'two', 'two', 'one', 'three'],
'C' : np.random.randn(8),
'D' : np.random.randn(8)})
df_grouped = df.groupby('B')
for name, group in df_grouped:
new_df["D_diff"] = group["D"].diff()
:のようなもの。
これは私がやったであろうものです。 – piRSquared