coolleaf
coolleaf

Reputation: 25

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.

Upvotes: 1

Views: 331

Answers (1)

JohnE
JohnE

Reputation: 30424

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.

Upvotes: 1

Related Questions