python - Pandas - custom rolling concatenation function per each group -
i have dataframe 3 columns
- crop year
- base_field_name
- meta_crop
i want per each base_field_name
per each of rows cumulative (rolling) concatenation of last 3 rows in meta_crop
column.
trying use groupby, apply , rolling function cant make work relevant snippet.
def get_name_from_list(str_list): print(str_list) if len(str_list) == 0: return '' else: return '_'.join([str(y) y in str_list if y]) def get_last_x_years_planting(x_years, data_ser): ## data ser [(year, crop)] - assuming sorted ascending. return data_ser.rolling(window=x_years, on='meta_crop').apply(lambda x: get_name_from_list(x)) work_df.sort_values(by='crop_year', ascending=true).groupby('base_field_name').apply(lambda x: get_last_x_years_planting(3, x))
i "typeerror - float required" error.
Comments
Post a Comment