0

I have a data set which looks like follows (Values changed):

Value   A   B
ABC     20  5
ABC     20  5 
ABC     20  5
ABC     20  5
XYZ     20  5
XYZ     20  5
XYZ     20  5

I create a new column C using

df['C'] = df['A']/df['B']

I obtain the df:

Value   A   B   C
ABC     20  5       4
ABC     20  5       4
ABC     20  5       4
ABC     20  5       4
XYZ     20  5       4
XYZ     20  5       4
XYZ     20  5       4
XYZ     20  5       4

Until here everything works fine. Now When I try to use group by on the df, it doesn't give the sum of the third column(the one I created). I am using the following code line for group by

df.groupby('Value').sum()

The result is:

Value   A   B
ABC    60   15
XYZ    60   15

Any reason why it might be doing that?

A.DS
  • 216
  • 1
  • 4
  • 14
  • Doing what? `print(df.groupby('Value').sum())` works just fine for me. – Mr. T Jun 08 '18 at 08:50
  • 1
    You need to assign your `groupby` back to your dataframe, i.e. `df = df.groupby('Value').sum()`. `groupby` is not an in-place calculation. – jpp Jun 08 '18 at 08:53
  • @jpp Why would that be a specific problem for column C? The OP has to elaborate, what doesn't work. – Mr. T Jun 08 '18 at 08:57
  • @Mr. T I have not shared the original data set. But basically it isn't giving me the sum of columns created by me/ or which weren't there in the df originally. – A.DS Jun 08 '18 at 08:59
  • @A.DS But is the problem reproducible with your toy dataset in the question? If not, people can't suggest solutions. – Mr. T Jun 08 '18 at 09:00
  • @Mr. T Makes sense. Unfortunately unable to replicate a data set, which would behave similarly. – A.DS Jun 08 '18 at 09:18
  • 1
    Well, a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) is necessary to analyze the problem. Though it is difficult to understand, why you can't copy a couple of rows from your dataset and change the values, if the data are not meant to be published. And no error messages? What does appear in `C` then? Maybe this will give people an idea, what the underlying problem is. – Mr. T Jun 08 '18 at 09:23
  • @Mr. T It is just eliminating the columns that I created. Modified the post as well for a clearer picture. – A.DS Jun 08 '18 at 10:03
  • 2
    Sounds like this problem here: https://stackoverflow.com/q/37575944/8881141 – Mr. T Jun 08 '18 at 10:09

0 Answers0