From 9f82d9e0f4c34979d6bf5c5bc3a2edaa835fd987 Mon Sep 17 00:00:00 2001 From: wangwenyan Date: Fri, 16 Aug 2024 11:06:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=88=90=E4=BA=A4=E6=8C=82?= =?UTF-8?q?=E7=89=8C=E4=BB=B7=E7=B3=BB=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dags/business/business/evaluate_ratio.py | 36 +++++++++++++----------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/dags/business/business/evaluate_ratio.py b/dags/business/business/evaluate_ratio.py index a9ab4fb..1053769 100644 --- a/dags/business/business/evaluate_ratio.py +++ b/dags/business/business/evaluate_ratio.py @@ -2,7 +2,7 @@ from goojia_utils import MAIN_MONGO_CLIENT, dateutils from datetime import datetime, date import pandas as pd from dateutil.relativedelta import relativedelta - +import logging deal_price_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'deal_price') district_sell_stat_conn = MAIN_MONGO_CLIENT.get_collection('fangjia_stat', 'district_sell_stat') evaluate_ratio_conn = MAIN_MONGO_CLIENT.get_collection('business', 'evaluate_ratio') @@ -47,8 +47,8 @@ def sync(): # 过滤数据(同小区成交价与小区成交均价偏差较大) df1['中位值'] = df1.groupby(by=['city', 'region', 'district_name'])['avg_price'].transform("median") df1['ratio'] = round((df1['avg_price'] - df1['中位值']) / df1['中位值'], 2) - df1 = df1.drop(df1.index[df1['ratio'] > 0.2]) - df1 = df1.drop(df1.index[df1['ratio'] < -0.2]) + df1 = df1.drop(df1.index[df1['ratio'] > 0.25]) + df1 = df1.drop(df1.index[df1['ratio'] < -0.25]) # 从日期中提取年月,进行拼接 df1['Time'] = pd.to_datetime(df1['trade_date']) @@ -58,22 +58,24 @@ def sync(): convert_dict = {'date': int} df1 = df1.astype(convert_dict) + year_month = int(dateutils.format_date(datetimeNow, fm='%Y%m')) + two_year_ago_year_month = int(dateutils.format_date(datetimeTwoYearAgo, fm='%Y%m' )) # 查询2年内挂牌价格 district_stat_r = district_sell_stat_conn.find({ 'date': { - "$gte": 202106, - "$lt": 202306 + "$gte": year_month, + "$lt": two_year_ago_year_month } }, {'city': 1, 'region': 1, 'district_name': 1, 'avg_price': 1, 'date': 1}) - + logging.info(f"2C价格查询范围{year_month}-{two_year_ago_year_month}") df2 = pd.DataFrame(list(district_stat_r)) # 过滤数据(同小区挂牌价与小区挂牌均价偏差较大) df2['中位值'] = df2.groupby(by=['city', 'region', 'district_name'])['avg_price'].transform("median") df2['ratio'] = round((df2['avg_price'] - df2['中位值']) / df2['中位值'], 2) - df2 = df2.drop(df2.index[df2['ratio'] > 0.2]) - df2 = df2.drop(df2.index[df2['ratio'] < -0.2]) + df2 = df2.drop(df2.index[df2['ratio'] > 0.25]) + df2 = df2.drop(df2.index[df2['ratio'] < -0.25]) # 合并表 df1_2 = pd.merge(df1, df2, how='left', on=['city', 'region', 'district_name', 'date']) @@ -81,8 +83,8 @@ def sync(): # 过滤数据(成交价与挂牌价偏差较大) df1_2['ratio'] = round((df1_2['avg_price_x'] - df1_2['avg_price_y']) / df1_2['avg_price_y'], 2) - df1_2 = df1_2.drop(df1_2.index[df1_2['ratio'] > 0.2]) - df1_2 = df1_2.drop(df1_2.index[df1_2['ratio'] < -0.2]) + df1_2 = df1_2.drop(df1_2.index[df1_2['ratio'] > 0.25]) + df1_2 = df1_2.drop(df1_2.index[df1_2['ratio'] < -0.25]) df1_2 = df1_2.dropna(axis='index', how='all', subset=['ratio']) # 城市挂牌价与成交价计算价格系数 @@ -97,12 +99,12 @@ def sync(): for index in df.index: city = df['city'].get(index) ratio = df['ratio'].get(index) - if ratio > 0.15 or ratio < -0.15: + if ratio > 0.25 or ratio < -0.25: continue else: query = {"city": city, "category": "city"} update = {"$set": {"ratio": ratio, "m_date": m_date}} - update_result = evaluate_ratio_conn.update_one(query, update, upsert=True) + evaluate_ratio_conn.update_one(query, update, upsert=True) # 区域挂牌价与成交价计算价格系数 deal_price_region_avgprice = df1_2.groupby(by=['city', 'region'])['avg_price_x'].mean().reset_index() @@ -114,12 +116,12 @@ def sync(): city = df['city'].get(index) ratio = df['ratio'].get(index) region = df['region'].get(index) - if ratio > 0.15 or ratio < -0.15: + if ratio > 0.25 or ratio < -0.25: continue else: query = {"city": city, "region": region, "category": "region"} update = {"$set": {"ratio": ratio, "region": region, "m_date": m_date}} - update_result = evaluate_ratio_conn.update_one(query, update, upsert=True) + evaluate_ratio_conn.update_one(query, update, upsert=True) # 小区挂牌价与成交价计算价格系数 deal_price_district_avgprice = df1_2.groupby(by=['city', 'region', 'district_name'])[ @@ -129,20 +131,20 @@ def sync(): df = pd.merge(deal_price_district_avgprice, district_stat_district_avgprice, how='inner', on=['city', 'region', 'district_name']) df['ratio'] = round((df['avg_price_x'] - df['avg_price_y']) / df['avg_price_y'], 2) - df[['city', 'region', 'district_name', 'ratio']] + df = df[['city', 'region', 'district_name', 'ratio']] for index in df.index: city = df['city'].get(index) ratio = df['ratio'].get(index) region = df['region'].get(index) district_name = df['district_name'].get(index) - if ratio > 0.15 or ratio < -0.15: + if ratio > 0.25 or ratio < -0.25: continue else: query = {"city": city, "region": region, "district_name": district_name, "category": "district"} update = { "$set": {"ratio": ratio, "m_date": m_date}} - update_result = evaluate_ratio_conn.update_one(query, update, upsert=True) + evaluate_ratio_conn.update_one(query, update, upsert=True) sync()