Home > Net >  How to append value from large dataframe to a smaller value for summart statistics?
How to append value from large dataframe to a smaller value for summart statistics?

Time:04-26

I have have a larger datafram of values for vehicles and speed, etc, etc.

I want to create smaller dataframe of which only contains vehicle and max speed for each car.

My code so far:

df = pd.read_csv('larger_dataset.csv')

Speed = pd.DataFrame(df['vehicle'].unique(), columns = ['vehicle']
Speed['Max']=""

for car in Speed['vehicle']:
    data = df.loc[df['vehicle'] == car]
    max_sp = data['speed'].max()
    Speed['Max'].append(max_sp)

When I run the code I get this error:

TypeError: cannot concatenate object of type '<class 'numpy.int64'>'; only Series and DataFrame objs are valid

Speed is just a list of all unique vehicles. I can also get the max speed value for all vehicles from df, but I guess my issue is I dont know how to appropriately append that value to the Speed.

Thank you for any help.

CodePudding user response:

max_sp is a number so you can't append it a dataframe. you can key into your Speed dataframe and set the max for each car you have or you try groupby

speed_df = df.groupby(['vehicle'], sort=False)['speed'].max()

  • Related