首页 > 解决方案 > 如何将列数据类型从浮点数转换为数据框中的字符串

问题描述

如何修复此错误:

Traceback (most recent call last):
  ....
sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', '[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Error converting data type nvarchar to float. (8114) (SQLExecDirectW)')
[SQL: INSERT INTO all_sites_he (vendor, site_id) VALUES (?, ?)]
[parameters: (('ESN', 4), ('ESN', 5), ('ESN', 7), ('ESN', 8), ('ESN', 9), ('ESN', 10), ('ESN', 11), ('ESN', 12)  ... displaying 10 of 500 total bound parameter sets ...  ('ESN', 723), ('HUA', 724))]
(Background on this error at: http://sqlalche.me/e/f405)

Process finished with exit code 1

这是我的代码部分:

    def prepare_data():
        """Create DataFrame and clean column names."""
        jobs_DF = pd.read_excel(os.path.join(os.path.dirname(__file__),all_sitesexcel), usecols=['Vendor','Site_ID'], sheet_name='Sheet2')
        new_columns = [column.replace(' ', '_').lower() for column in jobs_DF]
        jobs_DF.columns = new_columns
        kanban_sites = 'Site Integration Kanban_Netherlands KPN Mobile Network Modernization Project__20200207225328.xlsx'
        hua_sites_df = pd.read_excel(kanban_sites, sheet_name='gsclist1', header=1)
        jobs_DF['vendor'] = jobs_DF["vendor"].round(4).apply(str)
        jobs_DF['vendor'] = np.where(jobs_DF.site_id.isin(hua_sites_df['Site ID']),
                                             "HUA", "ESN")
        return jobs_DF

因此,在阅读此数据框后,我只需要更新列vendor

那我该如何解决这个错误呢?

标签: pythonpandas

解决方案


推荐阅读