Home > Mobile >  Pandas Dataframe fransform according to condition
Pandas Dataframe fransform according to condition

Time:12-09

I have the following dataframe and would like to transform the values of the dataframe according to the function bellow, but I can't get it to work with .transform nor with .apply.

dgp_id        8    13
lasso_class  4.0  2.0
lasso_reg    3.0  1.0
rf_class     1.0  4.0
rf_reg       2.0  3.0
xgb_class    6.0  6.0
xgb_reg      5.0  5.0
def scoring(x):
    if x == 1.0:
        x = 1
    elif x == 2.0:
        x=1/2
    elif x == 5.0:
        x=-1/2
    elif x==6.0:
        x=-1
    else:
        x=0

CodePudding user response:

Assuming the column you want to transform has name "mycol", you could replace it explicitly:

newvals = [scoring(item) for item in df['mycol']]
df['mycol'] = newvals
  • also note your scoring function needs "return x" at the bottom

CodePudding user response:

IIUC, you can use a combination of transform, map, and fillna to create a column based change like this.

  1. transform will apply a function to each of our specified columns
  2. map will change values for us according to a dictionary
  3. fillna will take care of the fallthrough case- where anything not in the map dictionary will be replaced this value
# create a mapping of old values to new values
scoring = {1.0: 1, 2.0: 1/2, 5.0: -1/2, 6.0: -1}

new_df = (
    df.filter(["8", "13"]) # select columns to score
    .transform(lambda column: column.map(scoring)) # convert values
    .fillna(0)             # fill in fallthrough cases
)

print(new_df)
     8   13
0  0.0  0.5
1  0.0  1.0
2  1.0  0.0
3  0.5  0.0
4 -1.0 -1.0
5 -0.5 -0.5

If you then want to combine this with the original data, you can use .join like so:


full_df = df.join(new_df, rsuffix="_scored")

print(full_df)
        dgp_id    8   13  8_scored  13_scored
0  lasso_class  4.0  2.0       0.0        0.5
1    lasso_reg  3.0  1.0       0.0        1.0
2     rf_class  1.0  4.0       1.0        0.0
3       rf_reg  2.0  3.0       0.5        0.0
4    xgb_class  6.0  6.0      -1.0       -1.0
5      xgb_reg  5.0  5.0      -0.5       -0.5
  • Related