编程小白艾雪儿 发表于 2022-6-15 11:21:41

python筛选数据

本帖最后由 编程小白艾雪儿 于 2022-6-15 11:23 编辑

想要按 “发行人省份.xlsx” 中的不同省份,对 ”新发行债券_20220610.xlsx“ 进行筛选,
但在进行匹配的第16行” FF = data.loc == rowdate] ” 出现了错误,还找不到原因555

import pandas as pd
import xlrd

dir = r"C:\Users\liqian6\Desktop\RPA工作\全量表数据&母子公司对应关系"#设置工作路径

#选择发行人省份.xlsx的表,按其第一列的数对新发行债券_20220610.xlsx进行筛选
workxls =xlrd.open_workbook(dir+"\\"+'发行人省份.xlsx')
worksheet = workxls.sheet_by_name("Sheet1")
row =worksheet.nrows#总行数
for i in range(1,2):
    rowdate =worksheet.row_values(i)
    print(rowdate)
   
    #读取新发行债券_20220610.xlsx,按不同省份对其进行筛选
    data = pd.read_excel(dir + "\\" + '新发行债券-0610\新发行债券_20220610.xlsx')
    FF = data.loc == rowdate]   

    FF.to_excel(dir + "\\" + rowdate + '_20220610.xlsx', engine='xlsxwriter', index = False)
   
    print( rowdate+' is successful')

错误情况:
Traceback (most recent call last):

File "C:\Users\liqian6\Desktop\python\python办公自动化_应用\01excel\2. excel筛选数据形成新表.py", line 20, in <module>
    FF = data.loc == rowdate]

File "C:\Users\liqian6\Anaconda3\lib\site-packages\pandas\core\ops\common.py", line 69, in new_method
    return method(self, other)

File "C:\Users\liqian6\Anaconda3\lib\site-packages\pandas\core\arraylike.py", line 32, in __eq__
    return self._cmp_method(other, operator.eq)

File "C:\Users\liqian6\Anaconda3\lib\site-packages\pandas\core\series.py", line 5502, in _cmp_method
    res_values = ops.comparison_op(lvalues, rvalues, op)

File "C:\Users\liqian6\Anaconda3\lib\site-packages\pandas\core\ops\array_ops.py", line 261, in comparison_op
    raise ValueError(

ValueError: ('Lengths must match to compare', (9370,), (1,))
页: [1]
查看完整版本: python筛选数据