请帮我为ai替代率匹配投资水平、敏捷程度和高管背景,这三个都开一列,匹配的条件是年份和股票代码,对于...
Erstellt am: 17. Juni 2025
Erstellt am: 17. Juni 2025
pythonos.listdir('/mnt/data') ['企业ai替代率.xlsx', '敏捷度.xlsx', 'A股上市公司人工智能投资水平(剔除st及金融).xlsx', '高管数字化背景.xlsx']
python# Attempt to load with pandas to preview first few rows and columns ai_invest_path = '/mnt/data/A股上市公司人工智能投资水平(剔除st及金融).xlsx' ai_replace_path = '/mnt/data/企业ai替代率.xlsx' agility_path = '/mnt/data/敏捷度.xlsx' exec_bg_path = '/mnt/data/高管数字化背景.xlsx' ai_invest_df = pd.read_excel(ai_invest_path, nrows=5) ai_replace_df = pd.read_excel(ai_replace_path, nrows=5) agility_df = pd.read_excel(agility_path, nrows=5) exec_bg_df = pd.read_excel(exec_bg_path, nrows=5) (ai_invest_df.head(), ai_replace_df.head(), agility_df.head(), exec_bg_df.head())
pythonai_invest_df.head() 证券代码 年份 证券简称 人工智能软件投资(元) 人工智能硬件投资(元) 人工智能总投资(元) 人工智能投资水平 0 2 2010 万科A NaN 5.901127e+07 5.901127e+07 0.0003 1 2 2011 万科A NaN 6.757055e+07 6.757055e+07 0.0002 2 2 2012 万科A NaN 7.217291e+07 7.217291e+07 0.0002 3 2 2013 万科A NaN 8.558682e+07 8.558682e+07 0.0002 4 2 2014 万科A NaN 1.069330e+08 1.069330e+08 0.0002
pythonai_replace_df = pd.read_excel(ai_replace_path, engine='openpyxl', nrows=5) ai_replace_df.head()
pythonwith open(ai_replace_path, 'rb') as f: head = f.read(200) head[:50] b'\xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00>\x00\x03\x00\xfe\xff\t\x00\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00M\x00\x00\x00\x01\x00'
pythonimport xlrd
python!pip -q install xlrd
pythonwith open(agility_path, 'rb') as f: print(f.read(8))
pythonagility_df = pd.read_excel(agility_path, engine='openpyxl', nrows=5) agility_df.head() symbol year board stock 0 1 2010 13 3 1 1 2011 9 3 2 1 2012 13 4 3 1 2013 9 2 4 1 2014 10 3
pythonwith open(exec_bg_path, 'rb') as f: print(f.read(8))
pythonexec_bg_df = pd.read_excel(exec_bg_path, engine='openpyxl', nrows=5) exec_bg_df.head() Stkcd Reptdt PersonID Major 0 证券代码 统计截止日期 人员ID 专业 1 000001 2010 3039342 0 2 000001 2010 3030656 0 3 000001 2010 3054556 0 4 000001 2010 3090144 0
pythontry: ai_replace_df = pd.read_excel(ai_replace_path, engine='pyxlsb', nrows=5) except Exception as e: print(e)
pythonagility_df.head() symbol year board stock 0 1 2010 13 3 1 1 2011 9 3 2 1 2012 13 4 3 1 2013 9 2 4 1 2014 10 3
pythonagility_df.sample(5) symbol year board stock 4 1 2014 10 3 3 1 2013 9 2 0 1 2010 13 3 1 1 2011 9 3 2 1 2012 13 4
pythonagility_full = pd.read_excel(agility_path, engine='openpyxl') agility_full.shape (51626, 4)
pythonexec_bg_df2 = pd.read_excel(exec_bg_path, engine='openpyxl', header=1) exec_bg_df2.head() 证券代码 统计截止日期 人员ID 专业 0 1 2010 3039342 0 1 1 2010 3030656 0 2 1 2010 3054556 0 3 1 2010 3090144 0 4 1 2010 309382 NaN
pythonagility_full.columns Index(['symbol', 'year', 'board', 'stock'], dtype='object')
pythonwith ExcelFile(agility_path, engine='openpyxl') as xls: print(xls.sheet_names)
pythonagility_full[['board','stock']].describe() board stock count 51612.000000 51624.000000 mean 9.489828 3.318786 std 3.949262 1.776647 min 0.000000 1.000000 25% 7.000000 2.000000 50% 9.000000 3.000000 75% 11.000000 4.000000 max 58.000000 33.000000
我查看了您上传的 4 个文件,发现有两点需要请您确认/补充,才能顺利把四套数据按“年份 + 股票代码”整合成一张可下载的表格:
《企业ai替代率.xlsx》文件的格式
“敏捷程度”应取哪一列/指标?
symbol (股票代码) / year / board / stock
四列,没有直接标注“敏捷度”数值。board
、或用 stock
、或二者之和/比例等?只要收到:
我就能立即把四表 按“年份+股票代码”外左连接,补齐缺失值为空,并生成可直接下载的 Excel/CSV 文件并在对话中提供下载链接。期待您的补充!
pythondata_dir = '/mnt/data' os.listdir(data_dir) ['敏捷度.xlsx', '企业ai替代率.xlsx', '高管数字化背景.xlsx', 'A股上市公司人工智能投资水平(剔除st及金融).xlsx']
python# Load the files and inspect minimal rows def try_read(path): try: df = pd.read_excel(path) return df.head() except Exception as e: return str(e) for fname in os.listdir(data_dir): print(fname) res = try_read(os.path.join(data_dir, fname)) print(res) print('---')
pythontry: df_ai = pd.read_excel('/mnt/data/企业ai替代率.xlsx', engine='openpyxl') except Exception as e: print("openpyxl error", e)
pythondf_mj = pd.read_excel('/mnt/data/敏捷度.xlsx') df_mj.head() symbol year board stock 0 1 2010 13.0 3.0 1 1 2011 9.0 3.0 2 1 2012 13.0 4.0 3 1 2013 9.0 2.0 4 1 2014 10.0 3.0
pythondf_gj = pd.read_excel('/mnt/data/高管数字化背景.xlsx') df_gj.head() Stkcd Reptdt PersonID Major 0 证券代码 统计截止日期 人员ID 专业 1 000001 2010 3039342 0 2 000001 2010 3030656 0 3 000001 2010 3054556 0 4 000001 2010 3090144 0
pythondf_gj2 = pd.read_excel('/mnt/data/高管数字化背景.xlsx', header=1) df_gj2.head() 证券代码 统计截止日期 人员ID 专业 0 1 2010 3039342 0 1 1 2010 3030656 0 2 1 2010 3054556 0 3 1 2010 3090144 0 4 1 2010 309382 NaN
我已经把 4 份文件在后台尝试读取并做了初步对齐,结果如下:
文件 | 读取情况 | 还需确认 / 处理 |
---|---|---|
A股上市公司人工智能投资水平(剔除ST及金融).xlsx(表 1) | ✅ 已成功读取。<br>关键列:<br>• 证券代码 → 建议统一补零为 6 位(如 2 →000002 )<br>• 年份 | — |
企业ai替代率.xlsx(表 2) | ❌ 依旧是 旧版 .xls 二进制格式(只是把扩展名改成 .xlsx)。pandas 需要 .xlsx / .csv 才能解析,当前环境无法安装 xlrd。 | 请重新保存为真正的 .xlsx 或 .csv 再上传。<br>操作:Excel → “另存为” → 文件类型选 “Excel 工作簿 (*.xlsx)” |
敏捷度.xlsx(表 3) | ✅ 能读取。<br>包含列:`symbol | year |
高管数字化背景.xlsx(表 4) | ✅ 能读取,但首行是中文列名,会把真实数据错位。已将第 2 行当列名后可正常解析:`证券代码 | 统计截止日期 |
收到这两点后,我会:
symbol
+ year
左连接到表 1(只匹配表 1 已有的组合,缺失留空)期待您的补充!