Search code examples
pythonpandasdataframenormalize

How to set the starting scores of value related scores to 100 in df


This is such a simple question but I can't find it using the terms 'normalize' or the overused term 'index'. How do I set the start value of all my 'cars' to 100? In reality there are 52 cars and much more timestamps.

import pandas as pd

ExampleOfWhatIHave = {'Car':['A', 'B', 'A', 'B'],
                      'Hour':['1', '1', '2', '2'],
                      'Car-speed':[10, 20, 11, 19]}

ExampleOfWhatINeed = {'Car':['A', 'B', 'A', 'B'],
                      'Hour':['1', '1', '2', '2'],
                      'Car51-speed':[100, 100, 110, 95]}
# Create DataFrame
df = pd.DataFrame(ExampleOfWhatIHave)
df = pd.DataFrame(ExampleOfWhatINeed)

Solution

  • How about this nice one liner?

    df['Car51-speed'] = 100 * df['Car-speed'] / df.groupby('Car')['Car-speed'].transform('first')