Home > Software engineering >  Copying and Replacing Values with Values Within the Same Variable Based on Condition
Copying and Replacing Values with Values Within the Same Variable Based on Condition

Time:03-02

Sorry if the title is not specific enough, I'm imagining it in excel terms. I have a dataframe:

           Product             Group             ...        Score_Alpha      Score_Beta
0           XXX0X1             Cinnamon          ...         0.007598         0.007538
1           XXX0X2             Cinnamon          ...         0.007598         0.007538
2           XXX0X3             Cinnamon          ...         0.007598         0.007538
3           XXX0X4             Cinnamon Special  ...         0.003343         0.002696
4           XXX0X5             Cinnamon Special  ...         0.003343         0.002696
5           XXX0X6             Cinnamon Special  ...         0.003343         0.002696
6           XXX0X7             Peach             ...         0.003399         0.004444
7           XXX0X8             Peach             ...         0.003399         0.004444
8           XXX0X9             Peach             ...         0.003399         0.004444
9           XXX0X10            Peach Special     ...         0.006677         0.006262
10          XXX0X11            Peach Special     ...         0.006677         0.006262
11          XXX0X12            Peach Special     ...         0.006677         0.006262

I need to replace the Score_Alpha and Score_Beta of lines where Group =='Cinnamon Special' with that of 'Cinnamon', the same between 'Peach Special' and 'Peach'. Basically, it should look like this:

           Product             Group             ...        Score_Alpha      Score_Beta
0           XXX0X1             Cinnamon          ...         0.007598         0.007538
1           XXX0X2             Cinnamon          ...         0.007598         0.007538
2           XXX0X3             Cinnamon          ...         0.007598         0.007538
3           XXX0X4             Cinnamon Special  ...         0.007598         0.007538
4           XXX0X5             Cinnamon Special  ...         0.007598         0.007538
5           XXX0X6             Cinnamon Special  ...         0.007598         0.007538
6           XXX0X7             Peach             ...         0.003399         0.004444
7           XXX0X8             Peach             ...         0.003399         0.004444
8           XXX0X9             Peach             ...         0.003399         0.004444
9           XXX0X10            Peach Special     ...         0.003399         0.004444
10          XXX0X11            Peach Special     ...         0.003399         0.004444
11          XXX0X12            Peach Special     ...         0.003399         0.004444

My apologies if this sort of question has already been answered, my googling skills are questionable.

I have about 30 unique values in Group with their own 'XXX Special' counterparts so I cannot manually group by specific values in the variable

Thank you for reading!

CodePudding user response:

First, get the value of Score_Alpha and Score_Beta where Group =='Cinnamon'

scores = df.loc[df["Group"]=='Cinnamon',["Score_Alpha", "Score_Beta"]].iloc[0].tolist()

Second, put the scores to df where Group =='Cinnamon Special'

df.loc[df["Group"]=='Cinnamon Special', ["Score_Alpha", "Score_Beta"]] = scores

In the same way,

scores = df.loc[df["Group"]=='Peach',["Score_Alpha", "Score_Beta"]].iloc[0].tolist()
df.loc[df["Group"]=='Peach Special', ["Score_Alpha", "Score_Beta"]] = scores

Because there are 30 unique values in Group with their own 'XXX Special', you can solve it with a function

# First, extract all group names without "Special"
names = [x for x in set(df["Group"].tolist()) if "Special" not in x]

# Second, define a function
def replace_values(df, name):
    scores = df.loc[df["Group"] == name, ["Score_Alpha", "Score_Beta"]].iloc[0].tolist()
    df.loc[df["Group"]== name   ' Special', ["Score_Alpha", "Score_Beta"]] = scores

# Third, iterate name in names
for name in group:
    replace_values(df, name)
  • Related