Home > Mobile >  What is the write way to assign new values to a slice of dataset and return back the whole dataset
What is the write way to assign new values to a slice of dataset and return back the whole dataset

Time:11-17

I have a pandas dataframe that I want to transform some columns and assign back the transformation into the existing dtaframe:

df.loc[:,df.columns != 'target'].pct_change(-1)

If I do:

df.loc[:,df.columns != 'target'] = df.loc[:,df.columns != 'target'].pct_change(-1)

I get a warning:

A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

What is the right to way to get back the transformed df ? thanks

CodePudding user response:

You can do update

df.update(df.loc[:,df.columns != 'target'].pct_change(-1))

CodePudding user response:

Filter out columns that have word target in them using df.filter

df[df.filter(regex='[^target]').columns]=df.loc[:,df.filter(regex='[^target]').columns].pct_change(-1)
  • Related