Home > Blockchain >  How to update original array with groupby in python
How to update original array with groupby in python

Time:11-02

I have a dataset and I am trying to iterate each group and based on each group, I am trying to update original groups:

import pandas as pd
import numpy as np

arr = np.array([1, 2, 4, 7, 11, 16, 22, 29, 37, 46])
df = pd.DataFrame({'grain': ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B']})
df["target"] = arr

for group_name, b in df.groupby("grain"):
    if group_name == "A":
        // do some processing
    if group_name == "B":
        // do another processing

I expect to see original df is updated. Is there any way to do it?

CodePudding user response:

Here is a way to change the original data, this example requires a non-duplicate index. I am not sure what would be the benefit of this approach compared to using classical pandas operations.

import pandas as pd
import numpy as np
arr = np.array([1, 2, 4, 7, 11, 16, 22, 29, 37, 46])
df = pd.DataFrame({'grain': ['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B']})
df["target"] = arr
for g_name, g_df in df.groupby("grain"):
    if g_name == "A":
        df.loc[g_df.index, 'target'] *= 10
    if g_name == "B":
        df.loc[g_df.index, 'target'] *= -1

Output:

>>> df
  grain  target
0     A      10
1     B      -2
2     A      40
3     B      -7
4     A     110
5     B     -16
6     A     220
7     B     -29
8     A     370
9     B     -46
  • Related