spc963
spc963

Reputation: 31

How to perform an operation on specific columns within pandas dataframe while preserving other columns

I am trying to perform an operation on all columns within my pandas dataframe except one.

I first tried:

df=df[:,1:]*0.001

returned an error message. Then tried:

df=df.iloc[:,1:]*0.001

I want the operation to be performed on all columns except the first one. However, this replaces the dataframe with only the columns I have selected for the operation.

How do you select specific columns to perform operations while leaving others unaltered? I may have situations where it is not necessarily the first column to remain untouched.

Thanks.

Upvotes: 3

Views: 5917

Answers (3)

rty
rty

Reputation: 1

this is my solution :

  df.iloc[:, 1:] = df.iloc[:, 1:] * 0.001

the first column of df will remain untouched, while the rest is multiplied by 0.001

Upvotes: 0

ababuji
ababuji

Reputation: 1731

import pandas as pd

data = [[1,10, 5],[2,12,6],[3,13,7]]
df = pd.DataFrame(data,columns=['Name','Age', 'ID'])
print(df)

      Name  Age  ID
0     1     10   5
1     2     12   6
2     3     13   7

colnames = list(df.columns)

print(colnames)

['Name', 'Age', 'ID']

colnames.pop(0)


for i in colnames:

    df[i]  = df[i]*0.01

print(df)

    Name    Age     ID
0   1       0.10    0.05
1   2       0.12    0.06
2   3       0.13    0.07

The first column is left untouched. Although this may not be the best way to do it, it does what you ask

If you want a specific column ignored then,

in the line colnames.pop(0) replace 0 with the index of column. Over here, since Name column is in index 0, I did colnames.pop(0).

Upvotes: 1

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210842

Demo:

In [87]: d = pd.DataFrame(np.arange(25).reshape(5,5)).add_prefix('col')

In [88]: d
Out[88]:
   col0  col1  col2  col3  col4
0     0     1     2     3     4
1     5     6     7     8     9
2    10    11    12    13    14
3    15    16    17    18    19
4    20    21    22    23    24

In [89]: d.loc[:, d.columns != 'col2'] *= 0.01

In [90]: d
Out[90]:
   col0  col1  col2  col3  col4
0  0.00  0.01     2  0.03  0.04
1  0.05  0.06     7  0.08  0.09
2  0.10  0.11    12  0.13  0.14
3  0.15  0.16    17  0.18  0.19
4  0.20  0.21    22  0.23  0.24

Upvotes: 1

Related Questions