120 lines
5.5 KiB
Python
120 lines
5.5 KiB
Python
"""
|
|
户均总价根据房源面积补充至206
|
|
"""
|
|
import logging
|
|
import pandas as pd
|
|
from goojia_utils import ElasticSearchClient, dateutils
|
|
from elasticsearch import helpers
|
|
import sys
|
|
|
|
log = logging.getLogger(__name__)
|
|
es206_client = ElasticSearchClient(host='192.168.0.206')
|
|
|
|
index_name = "community_summary"
|
|
path = "/opt/airflow/dags/repo/dags/business/business/community/data"
|
|
m_date = dateutils.iso_format()
|
|
|
|
|
|
def build_doc(item):
|
|
doc_dict = dict()
|
|
if 'total_price' in item and pd.notna(item['total_price']):
|
|
doc_dict['total_price'] = int(item['total_price'])
|
|
return doc_dict
|
|
|
|
|
|
def save(community_factor_df):
|
|
community_factor_df = community_factor_df[community_factor_df['total_price'].notna()]
|
|
community_factor_df['total_price'] = community_factor_df['total_price'].astype(int)
|
|
actions = list()
|
|
count = 0
|
|
for index, item in community_factor_df.iterrows():
|
|
doc_info = build_doc(item)
|
|
|
|
action = {
|
|
"_op_type": "update",
|
|
"_index": index_name,
|
|
"_id": item['_id'],
|
|
"doc": doc_info,
|
|
"_retry_on_conflict": 5
|
|
}
|
|
|
|
actions.append(action)
|
|
if len(actions) == 5000:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
count += len(actions)
|
|
logging.info("已补全:" + str(count))
|
|
actions.clear()
|
|
if len(actions) > 0:
|
|
helpers.bulk(es206_client.elastic_client, actions)
|
|
actions.clear()
|
|
|
|
|
|
def complete_by_build_year_area(community_factor_df):
|
|
build_year_area_df = pd.read_excel(path + '/全国各区各建筑年代户均面积已去极值.xlsx')
|
|
build_year_area_dict = dict()
|
|
for index, build_year_area in build_year_area_df.iterrows():
|
|
build_year_area_dict[
|
|
build_year_area['city'] + '|' + build_year_area['region'] + "|" + build_year_area['buildyear']] = build_year_area['factor']
|
|
# 修复按城市跑批数据字段可能不存在问题
|
|
if 'finish_building_date' not in community_factor_df.columns or 'total_price' not in community_factor_df.columns:
|
|
return False
|
|
for index, item in community_factor_df[
|
|
(community_factor_df['total_price'].isna()) & (community_factor_df['finish_building_date'].notna())].iterrows():
|
|
finish_building_date = item['finish_building_date']
|
|
if finish_building_date < 1980:
|
|
community_factor_df.loc[index, 'factor'] = build_year_area_dict[
|
|
item['city'] + "|" + item['region'] + "|1980以前"] if item['city'] + "|" + item[
|
|
'region'] + "|1980以前" in build_year_area_dict else None
|
|
elif 1980 <= finish_building_date < 1990:
|
|
community_factor_df.loc[index, 'factor'] = build_year_area_dict[
|
|
item['city'] + "|" + item['region'] + "|[1980-1990)"] if item['city'] + "|" + item[
|
|
'region'] + "|[1980-1990)" in build_year_area_dict else None
|
|
elif 1990 <= finish_building_date < 2000:
|
|
community_factor_df.loc[index, 'factor'] = build_year_area_dict[
|
|
item['city'] + "|" + item['region'] + "|[1990-2000)"] if item['city'] + "|" + item[
|
|
'region'] + "|[1990-2000)" in build_year_area_dict else None
|
|
elif 2000 <= finish_building_date < 2010:
|
|
community_factor_df.loc[index, 'factor'] = build_year_area_dict[
|
|
item['city'] + "|" + item['region'] + "|[2000-2010)"] if item['city'] + "|" + item[
|
|
'region'] + "|[2000-2010)" in build_year_area_dict else None
|
|
elif 2010 <= finish_building_date < 2020:
|
|
community_factor_df.loc[index, 'factor'] = build_year_area_dict[
|
|
item['city'] + "|" + item['region'] + "|[2010-2020)"] if item['city'] + "|" + item[
|
|
'region'] + "|[2010-2020)" in build_year_area_dict else None
|
|
elif finish_building_date >= 2020:
|
|
community_factor_df.loc[index, 'factor'] = build_year_area_dict[
|
|
item['city'] + "|" + item['region'] + "|2020及以后"] if item['city'] + "|" + item[
|
|
'region'] + "|2020及以后" in build_year_area_dict else None
|
|
community_factor_df['total_price'] = community_factor_df['avg_price'] * community_factor_df['factor']
|
|
|
|
|
|
def complete_by_region_area(community_factor_df):
|
|
region_area_df = pd.read_excel(path + '/全国各区户均面积已去极值.xlsx')
|
|
region_area_dict = dict()
|
|
for index, region_area in region_area_df.iterrows():
|
|
region_area_dict[region_area['city'] + '|' + region_area['region']] = region_area['factor']
|
|
for index, item in community_factor_df[community_factor_df['total_price'].isna()].iterrows():
|
|
community_factor_df.loc[index, 'factor'] = region_area_dict[item['city'] + "|" + item['region']] if item['city'] + "|" + item['region'] in region_area_dict else None
|
|
community_factor_df['total_price'] = community_factor_df['avg_price'] * community_factor_df['factor']
|
|
|
|
|
|
def sync(city=None):
|
|
log.info(f"开始补全户均总价信息至206 {index_name}")
|
|
|
|
sql = "select city, region, name, avg_price, finish_building_date from community_summary where avg_price is not null and total_price is null "
|
|
if pd.notna(city):
|
|
sql += " and city='" + city + "'"
|
|
community_df = es206_client.scan_by_sql_to_data_frame(sql)
|
|
|
|
if len(community_df) > 0:
|
|
community_df['total_price'] = None
|
|
complete_by_build_year_area(community_df)
|
|
complete_by_region_area(community_df)
|
|
save(community_df)
|
|
|
|
log.info(f"完成补全户均总价信息至206 {index_name}")
|
|
|
|
|
|
city = sys.argv[1] if len(sys.argv) > 1 else None
|
|
sync(city)
|