From 2d7aaf647fa549a99871ee14f4d5cb72847b0117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E5=81=A5?= Date: Mon, 26 Aug 2024 09:55:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=85=B6=E4=BB=96=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E7=99=BE=E5=88=86=E4=BD=8D=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../business/business/community/percentile.py | 54 ++++++++----------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/dags/business/business/community/percentile.py b/dags/business/business/community/percentile.py index 9f466e8..bfdd8b6 100644 --- a/dags/business/business/community/percentile.py +++ b/dags/business/business/community/percentile.py @@ -22,34 +22,29 @@ def calc_percentile(city=None): sql += " where city='" + city + "'" community_summary_df = es206_client.scan_by_sql_to_data_frame(sql) - consumption_aim_df = community_summary_df[['id', 'city', 'avg_price', 'total_price']] + consumption_aim_df = community_summary_df[['id', 'city', 'avg_price', 'total_price', 'avg_cost', 'rent_price', 'relative_price_ranking', 'finish_building_date', + 'estate_charge_fix', 'household_count']] number = [] for i in range(1, 101): number.append(i / 100) percents_actions = list() + # 计算百分位的字段 + columns = ['avg_price', 'total_price', 'avg_cost', 'rent_price', 'relative_price_ranking', 'finish_building_date', + 'estate_charge_fix', 'household_count'] for city_now, item in consumption_aim_df.groupby(by=['city']).describe(percentiles=number).iterrows(): - for i in range(1, 101): - if str(i) + '%' in item['total_price']: - if pd.notna((item['total_price'])[str(i) + '%']): - action = {'city': city_now, 'type': 'total_price', - 'price': int((item['total_price'])[str(i) + '%']), 'percent': i} - else: - if pd.notna((item['total_price'])[str(i) + '.0%']): - action = {'city': city_now, 'type': 'total_price', - 'price': int((item['total_price'])[str(i) + '.0%']), 'percent': i} - percents_actions.append(action) - for i in range(1, 101): - if str(i) + '%' in item['avg_price']: - if pd.notna((item['avg_price'])[str(i) + '%']): - action = {'city': city_now, 'type': 'avg_price', 'price': (item['avg_price'])[str(i) + '%'], - 'percent': i} - else: - if pd.notna((item['avg_price'])[str(i) + '.0%']): - action = {'city': city_now, 'type': 'avg_price', 'price': (item['avg_price'])[str(i) + '.0%'], - 'percent': i} - percents_actions.append(action) + for column in columns: + for i in range(1, 101): + if str(i) + '%' in item[column]: + if pd.notna((item[column])[str(i) + '%']): + action = {'city': city_now, 'type': column, + 'price': int((item[column])[str(i) + '%']), 'percent': i} + else: + if pd.notna((item[column])[str(i) + '.0%']): + action = {'city': city_now, 'type': column, + 'price': int((item[column])[str(i) + '.0%']), 'percent': i} + percents_actions.append(action) percents_df = pd.DataFrame(percents_actions) @@ -64,13 +59,11 @@ def calc_percentile(city=None): try: for i in range(1, 101): if i == 1: - df.loc[(df['avg_price'] <= percents_now_df[(percents_now_df['type'] == 'avg_price') & (percents_now_df['percent'] == i)].iloc[0]['price']), 'avg_price_percentile'] = 101 - i - - df.loc[(df['total_price'] <= percents_now_df[(percents_now_df['type'] == 'total_price') & (percents_now_df['percent'] == i)].iloc[0]['price']), 'total_price_percentile'] = 101 - i + for column in columns: + df.loc[(df[column] <= percents_now_df[(percents_now_df['type'] == column) & (percents_now_df['percent'] == i)].iloc[0]['price']), column + '_percentile'] = 101 - i else: - df.loc[(df['avg_price'] > percents_now_df[(percents_now_df['type'] == 'avg_price') & (percents_now_df['percent'] == i - 1)].iloc[0]['price']) & (df['avg_price'] <= percents_now_df[(percents_now_df['type'] == 'avg_price') & (percents_now_df['percent'] == i)].iloc[0]['price']), 'avg_price_percentile'] = 101 - i - - df.loc[(df['total_price'] > percents_now_df[(percents_now_df['type'] == 'total_price') & (percents_now_df['percent'] == i - 1)].iloc[0]['price']) & (df['total_price'] <= percents_now_df[(percents_now_df['type'] == 'total_price') & (percents_now_df['percent'] == i)].iloc[0]['price']), 'total_price_percentile'] = 101 - i + for column in columns: + df.loc[(df[column] > percents_now_df[(percents_now_df['type'] == column) & (percents_now_df['percent'] == i - 1)].iloc[0]['price']) & (df[column] <= percents_now_df[(percents_now_df['type'] == column) & (percents_now_df['percent'] == i)].iloc[0]['price']), column + '_percentile'] = 101 - i except Exception as e: log.error("解析错误{}", e) continue @@ -78,10 +71,9 @@ def calc_percentile(city=None): for index, item in df.iterrows(): row = dict() _id = str(item["id"]) - if pd.notna(item['total_price_percentile']): - row['total_price_percentile'] = int(item['total_price_percentile']) - if pd.notna(item['avg_price_percentile']): - row['avg_price_percentile'] = int(item['avg_price_percentile']) + for column in columns: + if pd.notna(item[column + '_percentile']): + row[column + '_percentile'] = int(item[column + '_percentile']) action = {'_op_type': 'update', '_index': index_name,