From b70761874ac27a9efcc16c8ac20b98870a56e6ce Mon Sep 17 00:00:00 2001 From: wangwenyan Date: Wed, 18 Jun 2025 11:52:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=A5=BC=E7=9B=98=E8=AF=84?= =?UTF-8?q?=E7=BA=A7-=E4=BC=98=E5=8C=96=E6=8E=92=E5=90=8D=E7=AE=97?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dags/stat/community/community_rate.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/dags/stat/community/community_rate.py b/dags/stat/community/community_rate.py index c608b3a..c985ddf 100644 --- a/dags/stat/community/community_rate.py +++ b/dags/stat/community/community_rate.py @@ -219,14 +219,13 @@ def load_comms(city): seaweed = seaweed.reset_index() for field in ['age', "cubage_percent", "virescence_percent", "estate_charge"]: seaweed[field] = seaweed[field].apply(lambda x: str2number(x)) - na_num = len(seaweed[seaweed[field].isna()]) + len(seaweed[seaweed[field].isna()]) seaweed = fill_missing_by_around(seaweed, field) seaweed[field] = seaweed[field].fillna(seaweed[field].mean()) seaweed['age'] = seaweed['age'].apply(lambda x: int(x)) seaweed['cubage_percent'] = seaweed['cubage_percent'].apply(lambda x: round(x, 2)) seaweed['virescence_percent'] = seaweed['virescence_percent'].apply(lambda x: round(x, 2)) seaweed['estate_charge'] = seaweed['estate_charge'].apply(lambda x: round(x, 2)) - seaweed['age'] = seaweed['age'].astype(int) return seaweed @@ -346,6 +345,8 @@ def comms_rating(data): data[number_field] = data[number_field].apply(lambda x: threshold_95 if x >= threshold_95 else x) data[number_field] = data[number_field].apply(lambda x: threshold_5 if x <= threshold_5 else x) data[f'{number_field}_score'] = normalize_to_01(data[number_field]) + data['age'] = data['age'].apply(lambda x: int(x) if pd.notna(x) else x) + data['age'] = data['age'].astype(int) for category_field in ['estate_type']: category_score(data, category_field, estate_type_source_map) data['total_score'] = 0 @@ -515,14 +516,19 @@ def process_city(city): df_simple[f'block{key}_rank'] = None df_simple[f'block{key}_total_rank'] = None if df_simple['block'].notna().any(): - subset = df_simple[df_simple['block'].notna()] - df_simple.loc[subset.index, f'block{key}_rank'] = subset.groupby( - ['city', 'region', 'block'])[factor_score_field].rank(method='dense', ascending=False).astype(int) - df_simple.loc[subset.index, f'block{key}_total_rank'] = df_simple.loc[subset.index][f'block{key}_rank'].max() - - for score_factor in ['total_score', 'community_quality_score', 'community_price_score', - 'location_advantage_score', 'flowability_score', 'living_facilities_score', - 'education_score', 'hospital_score']: + buffer = [] + data_null = data[data['block'].isna()].copy() + data_not_null = data[data['block'].notna()].copy() + buffer.append(data_null) + if len(data_not_null) > 0: + data_not_null['region_block'] = data_not_null['region'] + "_" + data_not_null['block'] + for item in data_not_null['region_block'].unique(): + group = data_not_null[(data_not_null['region'] == str(item).split("_")[0])&(data_not_null['block'] == str(item).split("_")[1])].copy() + group[f'block{key}_rank'] = group.groupby(['city', 'region', 'block'])[factor_score_field].rank(method='min', ascending=False).astype(int) + group[f'block{key}_total_rank'] = group[f'block{key}_rank'].max() + buffer.append(group) + df_simple = pd.concat(buffer) + for score_factor in ['total_score', 'community_quality_score', 'community_price_score','location_advantage_score', 'flowability_score', 'living_facilities_score','education_score', 'hospital_score']: df_simple[score_factor] = df_simple[score_factor].apply(lambda x: round(x, 2)) df_simple['rent_ro_price_ratio'] = df_simple['rent_ro_price_ratio'].apply(lambda x: round(x, 2))