优化成交挂牌价系数
This commit is contained in:
@@ -2,7 +2,7 @@ from goojia_utils import MAIN_MONGO_CLIENT, dateutils
|
|||||||
from datetime import datetime, date
|
from datetime import datetime, date
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from dateutil.relativedelta import relativedelta
|
from dateutil.relativedelta import relativedelta
|
||||||
|
import logging
|
||||||
deal_price_conn = MAIN_MONGO_CLIENT.get_collection('fangjia', 'deal_price')
|
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')
|
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')
|
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['中位值'] = df1.groupby(by=['city', 'region', 'district_name'])['avg_price'].transform("median")
|
||||||
df1['ratio'] = round((df1['avg_price'] - df1['中位值']) / df1['中位值'], 2)
|
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.25])
|
||||||
df1 = df1.drop(df1.index[df1['ratio'] < -0.2])
|
df1 = df1.drop(df1.index[df1['ratio'] < -0.25])
|
||||||
|
|
||||||
# 从日期中提取年月,进行拼接
|
# 从日期中提取年月,进行拼接
|
||||||
df1['Time'] = pd.to_datetime(df1['trade_date'])
|
df1['Time'] = pd.to_datetime(df1['trade_date'])
|
||||||
@@ -58,22 +58,24 @@ def sync():
|
|||||||
convert_dict = {'date': int}
|
convert_dict = {'date': int}
|
||||||
df1 = df1.astype(convert_dict)
|
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年内挂牌价格
|
# 查询2年内挂牌价格
|
||||||
district_stat_r = district_sell_stat_conn.find({
|
district_stat_r = district_sell_stat_conn.find({
|
||||||
'date':
|
'date':
|
||||||
{
|
{
|
||||||
"$gte": 202106,
|
"$gte": year_month,
|
||||||
"$lt": 202306
|
"$lt": two_year_ago_year_month
|
||||||
}
|
}
|
||||||
}, {'city': 1, 'region': 1, 'district_name': 1, 'avg_price': 1, 'date': 1})
|
}, {'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 = pd.DataFrame(list(district_stat_r))
|
||||||
|
|
||||||
# 过滤数据(同小区挂牌价与小区挂牌均价偏差较大)
|
# 过滤数据(同小区挂牌价与小区挂牌均价偏差较大)
|
||||||
df2['中位值'] = df2.groupby(by=['city', 'region', 'district_name'])['avg_price'].transform("median")
|
df2['中位值'] = df2.groupby(by=['city', 'region', 'district_name'])['avg_price'].transform("median")
|
||||||
df2['ratio'] = round((df2['avg_price'] - df2['中位值']) / df2['中位值'], 2)
|
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.25])
|
||||||
df2 = df2.drop(df2.index[df2['ratio'] < -0.2])
|
df2 = df2.drop(df2.index[df2['ratio'] < -0.25])
|
||||||
|
|
||||||
# 合并表
|
# 合并表
|
||||||
df1_2 = pd.merge(df1, df2, how='left', on=['city', 'region', 'district_name', 'date'])
|
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['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.25])
|
||||||
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.dropna(axis='index', how='all', subset=['ratio'])
|
df1_2 = df1_2.dropna(axis='index', how='all', subset=['ratio'])
|
||||||
|
|
||||||
# 城市挂牌价与成交价计算价格系数
|
# 城市挂牌价与成交价计算价格系数
|
||||||
@@ -97,12 +99,12 @@ def sync():
|
|||||||
for index in df.index:
|
for index in df.index:
|
||||||
city = df['city'].get(index)
|
city = df['city'].get(index)
|
||||||
ratio = df['ratio'].get(index)
|
ratio = df['ratio'].get(index)
|
||||||
if ratio > 0.15 or ratio < -0.15:
|
if ratio > 0.25 or ratio < -0.25:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
query = {"city": city, "category": "city"}
|
query = {"city": city, "category": "city"}
|
||||||
update = {"$set": {"ratio": ratio, "m_date": m_date}}
|
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()
|
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)
|
city = df['city'].get(index)
|
||||||
ratio = df['ratio'].get(index)
|
ratio = df['ratio'].get(index)
|
||||||
region = df['region'].get(index)
|
region = df['region'].get(index)
|
||||||
if ratio > 0.15 or ratio < -0.15:
|
if ratio > 0.25 or ratio < -0.25:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
query = {"city": city, "region": region, "category": "region"}
|
query = {"city": city, "region": region, "category": "region"}
|
||||||
update = {"$set": {"ratio": ratio, "region": region, "m_date": m_date}}
|
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'])[
|
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',
|
df = pd.merge(deal_price_district_avgprice, district_stat_district_avgprice, how='inner',
|
||||||
on=['city', 'region', 'district_name'])
|
on=['city', 'region', 'district_name'])
|
||||||
df['ratio'] = round((df['avg_price_x'] - df['avg_price_y']) / df['avg_price_y'], 2)
|
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:
|
for index in df.index:
|
||||||
city = df['city'].get(index)
|
city = df['city'].get(index)
|
||||||
ratio = df['ratio'].get(index)
|
ratio = df['ratio'].get(index)
|
||||||
region = df['region'].get(index)
|
region = df['region'].get(index)
|
||||||
district_name = df['district_name'].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
|
continue
|
||||||
else:
|
else:
|
||||||
query = {"city": city, "region": region, "district_name": district_name, "category": "district"}
|
query = {"city": city, "region": region, "district_name": district_name, "category": "district"}
|
||||||
update = {
|
update = {
|
||||||
"$set": {"ratio": ratio, "m_date": m_date}}
|
"$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()
|
sync()
|
||||||
|
|||||||
Reference in New Issue
Block a user