161 lines
8.7 KiB
Python
161 lines
8.7 KiB
Python
"""
|
|
财力评级统计
|
|
"""
|
|
from elasticsearch import helpers
|
|
from goojia_utils import ElasticSearchClient, dateutils, nextcloudutils, dingutils, data_utils
|
|
import pandas as pd
|
|
import numpy as np
|
|
import logging
|
|
import os
|
|
import sys
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
es206_client = ElasticSearchClient(host='192.168.0.89', username='elastic', password='elasticsearch')
|
|
index_name = "community_summary"
|
|
|
|
save_path_prefix = "/home/airflow/data/community"
|
|
|
|
save_png_path = save_path_prefix + "/consumption_rating"
|
|
if not os.path.exists(save_png_path):
|
|
os.makedirs(save_png_path)
|
|
nextcloud_aim_file_path = '财力评级/财力评级/财力评级统计报表'
|
|
|
|
|
|
def stat(community_summary_df):
|
|
grouped = community_summary_df.groupby(["city"])
|
|
|
|
results = []
|
|
for item, group in grouped:
|
|
# 计算财力评级指标
|
|
rating_counts = group["consumption_rating"].value_counts(normalize=True)
|
|
rating_1 = rating_counts.get(1, 0)
|
|
rating_2 = rating_counts.get(2, 0)
|
|
rating_3 = rating_counts.get(3, 0)
|
|
rating_4 = rating_counts.get(4, 0)
|
|
rating_5 = rating_counts.get(5, 0)
|
|
rating_6 = rating_counts.get(6, 0)
|
|
|
|
# 计算分数范围
|
|
rating_1_scores = group[group["consumption_rating"] == 1]["consumption_score"]
|
|
rating_2_scores = group[group["consumption_rating"] == 2]["consumption_score"]
|
|
rating_3_scores = group[group["consumption_rating"] == 3]["consumption_score"]
|
|
rating_4_scores = group[group["consumption_rating"] == 4]["consumption_score"]
|
|
rating_5_scores = group[group["consumption_rating"] == 5]["consumption_score"]
|
|
rating_6_scores = group[group["consumption_rating"] == 6]["consumption_score"]
|
|
|
|
# 计算置信度占比
|
|
confidence_counts = group["credit"].value_counts(normalize=True)
|
|
|
|
results.append({
|
|
"城市": item[0],
|
|
"财力评级1级占比": round(rating_1 * 100, 1),
|
|
"财力评级1级分数范围": f"{rating_1_scores.min():.0f}-{rating_1_scores.max():.0f}" if not rating_1_scores.empty else "-",
|
|
"财力评级2级占比": round(rating_2 * 100, 1),
|
|
"财力评级2级分数范围": f"{rating_2_scores.min():.0f}-{rating_2_scores.max():.0f}" if not rating_2_scores.empty else "-",
|
|
"财力评级3级占比": round(rating_3 * 100, 1),
|
|
"财力评级3级分数范围": f"{rating_3_scores.min():.0f}-{rating_3_scores.max():.0f}" if not rating_3_scores.empty else "-",
|
|
"财力评级4级占比": round(rating_4 * 100, 1),
|
|
"财力评级4级分数范围": f"{rating_4_scores.min():.0f}-{rating_4_scores.max():.0f}" if not rating_4_scores.empty else "-",
|
|
"财力评级5级占比": round(rating_5 * 100, 1),
|
|
"财力评级5级分数范围": f"{rating_5_scores.min():.0f}-{rating_5_scores.max():.0f}" if not rating_5_scores.empty else "-",
|
|
"财力评级6级占比": round(rating_6 * 100, 1),
|
|
"财力评级6级分数范围": f"{rating_6_scores.min():.0f}-{rating_6_scores.max():.0f}" if not rating_6_scores.empty else "-",
|
|
"高置信度占比": round(confidence_counts.get("高", 0) * 100, 1),
|
|
"中置信度占比": round(confidence_counts.get("中", 0) * 100, 1),
|
|
"低置信度占比": round(confidence_counts.get("低", 0) * 100, 1)
|
|
})
|
|
|
|
# 生成最终报表
|
|
report_df = pd.DataFrame(results)
|
|
aim_file = dateutils.format_date(dateutils.localize()) + '财力评级统计结果.csv'
|
|
report_df.to_csv(save_png_path + '/' + aim_file, index=False)
|
|
nextcloudutils.up_to_nextcloud(save_png_path + "/" + aim_file, nextcloud_aim_file_path + "/" + aim_file)
|
|
|
|
|
|
def detail(city):
|
|
"""
|
|
计算消费潜力
|
|
"""
|
|
if pd.notna(city):
|
|
query = {
|
|
"bool": {
|
|
"must": [
|
|
{
|
|
"terms": {
|
|
"city": [city]
|
|
}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
else:
|
|
query = {
|
|
"bool": {
|
|
"must": [
|
|
{
|
|
"match_all": {}
|
|
}
|
|
]
|
|
}
|
|
}
|
|
community_summary_df = es206_client.scan_to_data_frame(query={"query": query}, source=[
|
|
"id",
|
|
"city",
|
|
"region",
|
|
"name",
|
|
"avg_price",
|
|
"total_price",
|
|
"credit_detail.total_price_score",
|
|
"estate_charge",
|
|
"estate_charge_fix",
|
|
"estate_charge_flag",
|
|
"main_house_area",
|
|
"relative_price_ranking",
|
|
"avg_cost",
|
|
"consumption_rating",
|
|
"price_rating", "avg_price_calc_type",
|
|
"consumption_score",
|
|
"sub_factor.total_price_rating"
|
|
], index="community_summary")
|
|
# 减少重复查询
|
|
community_summary_df = data_utils.flatten_json(community_summary_df, 'sub_factor')
|
|
community_summary_df = data_utils.flatten_json(community_summary_df, 'credit_detail')
|
|
community_summary_df['diff'] = np.abs(community_summary_df['consumption_rating'] - community_summary_df['price_rating'])
|
|
community_summary_df['total_price_diff'] = np.abs(community_summary_df['consumption_rating'] - community_summary_df['total_price_rating'])
|
|
community_summary_df['estate_charge_type'] = np.where(
|
|
pd.notna(community_summary_df['estate_charge']), # 仅当 estate_charge 不为空时处理
|
|
np.where(
|
|
community_summary_df['estate_charge_flag'] == 1,
|
|
'周边',
|
|
'自有'
|
|
),
|
|
np.nan # 如果 estate_charge 为空,则返回 NaN
|
|
)
|
|
|
|
community_summary_df['credit'] = np.where(community_summary_df['total_price_diff'] == 0, '高', np.where(community_summary_df['total_price_diff'] == 1, '中', np.where(community_summary_df['total_price_diff'] >= 2, '低', '-')))
|
|
stat(community_summary_df)
|
|
community_summary_df = community_summary_df.rename(columns={'city': '城市', 'region': '区域', 'name': '小区名称', 'consumption_score': '财力分数', 'consumption_rating': '财力评级',
|
|
'total_price_rating': '户均总价评级', 'total_price_diff': '差值',
|
|
'total_price': '户均总价', 'avg_price': '小区均价', 'main_house_area': '典型面积', 'relative_price_ranking': '相对价格排名',
|
|
'avg_cost': '人均消费', 'estate_charge': '物业费', 'estate_charge_fix': '物业费修正', 'price_rating': '价格评级', 'estate_charge_type': '物业费获取方式',
|
|
'credit': '置信度'
|
|
})
|
|
community_summary_df['计算方式'] = np.where(community_summary_df['avg_price_calc_type'] == 1,'指导价',np.where(community_summary_df['avg_price_calc_type'] == 2,'自身样本',np.where(community_summary_df['avg_price_calc_type'] == 3,'第三方',
|
|
np.where(community_summary_df['avg_price_calc_type'] == 4,'交易数据',np.where(community_summary_df['avg_price_calc_type'] == 5,'周边楼盘',np.where(community_summary_df['avg_price_calc_type'] == 6,'推导', np.where(community_summary_df['avg_price_calc_type'] == 7,'周边推算','无方式')))))))
|
|
community_summary_df['获取方式'] = np.where(community_summary_df['total_price_score'] <= 6,'周边','自有')
|
|
aim_file = dateutils.format_date(dateutils.localize()) + '财力评级结果.csv'
|
|
community_summary_df.to_csv(save_png_path + '/' + aim_file, index=False, columns=["城市", "区域", "小区名称", "财力分数", "财力评级", "户均总价评级", "差值", "户均总价", "小区均价", "计算方式", "典型面积",
|
|
"获取方式", "相对价格排名", "人均消费", "物业费", "物业费获取方式", '置信度'])
|
|
nextcloudutils.up_to_nextcloud(save_png_path + "/" + aim_file, nextcloud_aim_file_path + "/" + aim_file)
|
|
|
|
dingutils.send_text("【统计】财力评级统计完毕,请点击 https://nextcloud.fangjia.com/s/nMbzk6ZKJDimapf 查看", "32400b949005817550937f82a82c064fe5be65c23204306442c15e8b485d7ef2")
|
|
|
|
|
|
sys_city = sys.argv[1] if len(sys.argv) > 1 else None
|
|
if pd.notna(sys_city) and "," in sys_city:
|
|
cities = sys_city.split(",")
|
|
for city in cities:
|
|
detail(city)
|
|
else:
|
|
detail(sys_city) |