0

I have a dataframe which looks similar to this:

Group    |     Value
-----------------------
 A       |      1
 A       |      4     
 A       |      5
 B       |      10      
 B       |      7
 B       |      15     

Basically, what I want to do is fill in the Value column with the smallest value based on the unique value given in the Group column, such that I get a dataframe like this:

Group    |     Value
-----------------------
 A       |      1
 A       |      1      
 A       |      1
 B       |      7      
 B       |      7
 B       |      7

i.e. since 1 is the smallest value for Group A, change everything to 1 and since 7 is the smallest value for Group B, change everything in Group B to 7

I'm not sure what to do for this, so any help would be appreciated!

1 Answers1

0

You can do it as follows:

  import pandas as pd
    
    # Create the example dataframe
    df = pd.DataFrame({'Group': ['A', 'A', 'A', 'B', 'B', 'B'], 'Value': [1, 4, 5, 10, 7, 15]})
    
    # Group the dataframe by 'Group' and find the minimum value in each group
    min_values = df.groupby('Group')['Value'].transform('min')
    
    # Assign the minimum values to the 'Value' column
    df['Value'] = min_values
    
    # Print the resulting dataframe
    print(df)
David Meu
  • 1,527
  • 9
  • 14