burnsa9
burnsa9

Reputation: 131

Dividing one column in a dataframe by a number while bringing back all other columns in the dataframe

I am trying to divide one column in a dataframe by a number while bringing back all other columns in a dataframe unchanged. The code below works for the division, but I dont know how to bring back the columns I wanted unchanged from the dataframe as well:

df= df[['C']].div(4, axis = 0)

the data looks like this

enter image description here

The output that I am looking for would be:

enter image description here

The closest I could find to an answer is here:Divide multiple columns by another column in pandas

However, the last quote says to use pd.set_index after the division, but I am not sure how that syntax is supposed to look.

Right now I am only getting the output column C not the two other columns.

Upvotes: 0

Views: 2341

Answers (1)

Vikika
Vikika

Reputation: 318

You can do:

df['C']= df['C']/4

It will divide the column C by 4 while keeping other columns same.

You are getting the output column C only because you are saving column C changed only:

df['C']= df[['C']].div(4, axis=0)

This may give you the exact result

Upvotes: 2

Related Questions