#python-3.x #pandas
#python-3.x #панды
Вопрос:
Привет всем, у меня есть этот фрейм данных. Я пытаюсь создать 2 дополнительных столбца, max_temperature и min_temperature, чтобы записать максимальное и минимальное значения температуры на основе stayid. Как я могу это сделать?
Ответ №1:
Попробуйте groupby, agg и pd.присоединяйтесь
newdf=(df.set_index('stayid')#Set stayid to allow joining of the aggregated to the main df .join(# This joins the ggregated df to main df df.groupby('stayid')['temp'].agg([min,max])# Compute the min and max temperature and put them into a summarised df ).rename(columns={'min':'min_temp', 'max':'max_temp'}))#rename the min and max columns)