Selecting/Manuplating cells based on their location in the dataframe

I have a dataframe as below

A   B   C   D   E   F   G   H   I
1   2   3   4   5   6   7   8   9
1   2   3   4   5   6   7   8   9
1   2   3   4   5   6   7   8   9
1   2   3   4   5   6   7   8   9
1   2   3   4   5   6   7   8   9
1   2   3   4   5   6   7   8   9
1   2   3   4   5   6   7   8   9

I want to multiply every 3rd column after the 2 column in the last 2 rows by 5 to get the ouput as below.

How to acomplish this?

A   B   C   D   E   F   G   H   I
1   2   3   4   5   6   7   8   9
1   2   3   4   5   6   7   8   9
1   2   3   4   5   6   7   8   9
1   2   3   4   5   6   7   8   9
1   2   3   4   5   6   7   8   9
1   10  3   4   25  6   7   40  9
1   10  3   4   25  6   7   40  9

I am able to select the cells i need with df.iloc[-2:,1::3] which results in the df as below but I am not able to proceed further.

B   E   H
2   5   8
2   5   8

I know that I can select the same cells with loc instead of iloc, then the calcualtion is straign forward, but i am not able to figure it out. The column names & cell values CANNOT Be used since these change (the df here is just a dummy data)



Read more here: https://stackoverflow.com/questions/66273295/selecting-manuplating-cells-based-on-their-location-in-the-dataframe

Content Attribution

This content was originally published by moys at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: