Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add/subtract value of a column to the entire column of the dataframe pandas

enter image description here

I have a DataFrame like this, where for column2 I need to add 0.004 throughout the column to get a 0 value in row 1 of column 2. Similarly, for column 3 I need to subtract 0.4637 from the entire column to get a 0 value at row 1 column 3. How do I efficiently execute this?

Here is my code –

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df2 = pd.DataFrame(np.zeros((df.shape[0], len(df.columns)))).round(0).astype(int)
for (i,j) in zip(range(0, 5999),range(1,len(df.columns))):
    if j==1:
        df2.values[i,j] = df.values[i,j] + df.values[0,1]
    elif j>1:
        df2.iloc[i,j] = df.iloc[i,j] - df.iloc[0,j]
print(df2)

Any help would be greatly appreciated. Thank you.

>Solution :

df2 = df - df.iloc[0]

Explanation:

Let’s work through an example.

df = pd.DataFrame(np.arange(20).reshape(4, 5))
0 1 2 3 4
0 0 1 2 3 4
1 5 6 7 8 9
2 10 11 12 13 14
3 15 16 17 18 19

df.iloc[0] selects the first row of the dataframe:

0    0
1    1
2    2
3    3
4    4
Name: 0, dtype: int64

This is a Series. The first column printed here is its index (column names of the dataframe), and the second one – the actual values of the first row of the dataframe.

We can convert it to a list to better see its values

df.iloc[0].tolist()
[0, 1, 2, 3, 4]

Then, using broadcasting, we are subtracting each value from the whole column where it has come from.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading