Home > Software engineering >  Add/subtract value of a column to the entire column of the dataframe pandas
Add/subtract value of a column to the entire column of the dataframe pandas

Time:08-12

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 -

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.

CodePudding user response:

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.

  • Related