Search code examples
pythonpandaspython-applymap

Modifying certain pandas dataframe colums, apply changes to entire original dataframe


I read a .csv file into a pandas dataframe delta_df = pd.read_csv('SAAF_121581_67_500_s.dat',index_col=False) - I need to apply the following operation to some of the columns based on the header of the column: delta_df.iloc[0:,2:].select(lambda x: not re.search('rad', x), axis=1)/1000

Essentially I am searching for each column where the header doesn't contain the string "rad" and dividing it's contents by 1000. So far so good.

From here I would like to apply delta_df.iloc[0:,2:].select(lambda x: not re.search('rad', x), axis=1)/1000 back into the original dataframe - changing the columns containing "rad", leaving all the rest unchanged.

I have tried to use the df.apply() but I think this might be a job for df.applymap() --> I can't seem to get it to work though.

Any suggestions or pointers would be greatly appreciated.


Solution

  • You could just store the columns, then use on both the right & left sides:

    cols = df.select(lambda x: not re.search('tick', x), axis=1).columns
    df[cols] = df[cols] / 1000.