You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

168 lines
6.1 KiB

import os
import pandas as pd
import re
# 文件路径
input_file = r'D:\计量经济学\计量实验资料及作业要求\计量实验资料及作业要求\图文帖子实验数据(新).xlsx'
output_file = r'D:\计量经济学\计量实验资料及作业要求\计量实验资料及作业要求\UGC回归数据.xlsx'
print("========================================")
print(" 使用pandas处理所有数据")
print("========================================")
print(f"输入文件: {input_file}")
print(f"输出文件: {output_file}")
print()
# 检查文件是否存在
if not os.path.exists(input_file):
print("错误: 输入文件不存在!")
exit(1)
print(f"输入文件大小: {os.path.getsize(input_file) / 1024:.2f} KB")
# 读取原始数据
try:
print("正在读取原始数据...")
df = pd.read_excel(input_file)
print(f"成功读取 {len(df)} 行数据")
print(f"列名: {list(df.columns)}")
# 识别列
print("\n识别列...")
helpfull_col = None
comment_count_col = None
comment_cols = []
for col in df.columns:
col_str = str(col).lower()
if 'helpfull' in col_str or 'helpful' in col_str:
helpfull_col = col
print(f"找到 Y 列 (helpfull): {col}")
elif '评论总数' in str(col) or '帖子评论总数' in str(col):
comment_count_col = col
print(f"找到 X1 列 (评论总数): {col}")
elif '评论' in str(col) and any(str(i) in str(col) for i in range(1, 6)):
comment_cols.append(col)
print(f"找到评论列 {len(comment_cols)}: {col}")
print(f"\n共找到 {len(comment_cols)} 个评论列")
# 创建回归数据
print("\n创建回归数据...")
regression_data = pd.DataFrame()
# Y (UGC有用性)
print("1. 计算 Y (UGC有用性)")
if helpfull_col:
regression_data['Y'] = pd.to_numeric(df[helpfull_col], errors='coerce').fillna(0)
else:
regression_data['Y'] = 0
# X1 (评论数量)
print("2. 计算 X1 (评论数量)")
if comment_count_col:
regression_data['X1'] = pd.to_numeric(df[comment_count_col], errors='coerce').fillna(0)
else:
regression_data['X1'] = 0
# 定义函数计算评论指标
def calculate_comment_metrics(row):
lengths = []
complexities = []
sentiments = []
richness = []
for col in comment_cols:
content = str(row.get(col, ''))
if content and content != 'None' and content != 'nan':
# 评论长度
lengths.append(len(content.replace(' ', '')))
# 评论复杂度
complexities.append(len(content.split()))
# 情感分析
positive_words = ['', '', '优秀', '喜欢', '满意', '', 'positive', 'good', 'great', 'excellent']
negative_words = ['', '糟糕', '不好', '失望', '不满', 'negative', 'bad', 'terrible', 'poor']
sentiment = 0
lower_content = content.lower()
if any(word in lower_content for word in positive_words):
sentiment = 1
elif any(word in lower_content for word in negative_words):
sentiment = -1
sentiments.append(sentiment)
# 信息丰富度
r = 0
if re.search(r'\d', content):
r += 1
if re.search(r'http[s]?://', content):
r += 1
if re.search(r'[\u2600-\u27BF]|[:;][-]?[)D]', content):
r += 1
richness.append(r)
return lengths, complexities, sentiments, richness
# 计算评论相关指标
print("3. 计算评论相关指标...")
comment_metrics = df.apply(calculate_comment_metrics, axis=1)
# X2: 评论长度平均值
print("4. 计算 X2 (评论长度)")
regression_data['X2'] = comment_metrics.apply(lambda x: sum(x[0]) / len(x[0]) if x[0] else 0)
# X3: 评论复杂度平均值
print("5. 计算 X3 (评论复杂度)")
regression_data['X3'] = comment_metrics.apply(lambda x: sum(x[1]) / len(x[1]) if x[1] else 0)
# X4: 评论可读性
print("6. 计算 X4 (评论可读性)")
regression_data['X4'] = regression_data.apply(lambda row: row['X2'] / row['X3'] if row['X3'] > 0 else 0, axis=1)
# X5: 内容情感性平均值
print("7. 计算 X5 (内容情感性)")
regression_data['X5'] = comment_metrics.apply(lambda x: sum(x[2]) / len(x[2]) if x[2] else 0)
# X6: 信息丰富度平均值
print("8. 计算 X6 (信息丰富度)")
regression_data['X6'] = comment_metrics.apply(lambda x: sum(x[3]) / len(x[3]) if x[3] else 0)
# 数据清洗
print("\n9. 数据清洗...")
for col in regression_data.columns:
regression_data[col] = pd.to_numeric(regression_data[col], errors='coerce').fillna(0)
# 验证数据
print("\n10. 验证数据...")
print(f"行数: {len(regression_data)}")
print(f"列数: {len(regression_data.columns)}")
print(f"列名: {list(regression_data.columns)}")
print(f"数据类型:")
print(regression_data.dtypes)
print(f"\n前5行数据:")
print(regression_data.head())
# 保存文件
print("\n11. 保存文件...")
regression_data.to_excel(output_file, index=False)
# 验证文件
print("\n12. 验证文件...")
if os.path.exists(output_file):
print(f"文件已成功保存: {output_file}")
print(f"文件大小: {os.path.getsize(output_file) / 1024:.2f} KB")
# 重新读取检查
df_check = pd.read_excel(output_file)
print(f"输出文件行数: {len(df_check)}")
print(f"输出文件列数: {len(df_check.columns)}")
else:
print("文件保存失败!")
print()
print("========================================")
print(" 任务完成")
print("========================================")
except Exception as e:
print(f"处理文件时出错: {str(e)}")
import traceback
traceback.print_exc()