我正在尝试合并一些.xls文件,并使用pandas将合并的内容导出到.csv文件中。一切正常,除了我有一个名为Label的列,其中包含一个字符串“NA”(缩写为不适用)。显然在最终的.csv中,我在预期NA的列中有Null。
我希望'NA'不是导致出口问题的某种保留词。我能够手动将包含“NA”的数据框导出到.csv文件中。这是代码。包含“NA”的列是Label。
import pandas as pd
import glob
from time import gmtime, strftime
path= input("Please enter the location of DataModel Spec/s:-> ")
GLB_DM_VER = input("Please enter global DM version:-> ")
GLB_DM_ENV = input("Please enter the global DM version environment:-> ")
datasource_id = "DSN_GMS"
dt=strftime("%Y%m%d%H%M", gmtime())
file_list = glob.glob(path+"\*.xls")
excels = [pd.ExcelFile(name) for name in file_list]
frames_standards = [x.parse(sheet_name='Data Model 0',
header=0,index_col=None) for x in excels]
frames_codelist = [y.parse(sheet_name='DataDictionaries',
header=0,index_col=None) for y in excels]
combined_std = pd.concat(frames_standards)
combined_std.insert(loc=0, column = 'DATASOURCE_ID', value = datasource_id )
combined_std.insert(loc=1, column = 'Global_DM_VERSION_ID', value =
GLB_DM_VER )
combined_std.insert(loc=2, column = 'Global_DM_VERSION_ENV', value =
GLB_DM_ENV )
combined_std['LOAD_ID'] = dt
combined_std['LENGTH'].replace(regex=True,inplace=True,to_replace=r'\'',value=r'')
#combined['LENGTH'] = combined['LENGTH'].str.replace("'","")
combined_codelist = pd.concat(frames_codelist)
order = len(combined_codelist.index)+1
ordr_range = list(range(1,order))
combined_codelist.insert(loc=0, column = 'DATASOURCE_ID', value = datasource_id )
combined_codelist.insert(loc=1, column = 'Global_DM_VERSION_ID', value = GLB_DM_VER )
combined_codelist.insert(loc=2, column = 'Global_DM_VERSION_ENV', value = GLB_DM_ENV )
combined_codelist_move = combined_codelist.pop('Decoded Value')
combined_codelist['LABEL']= combined_codelist_move
combined_codelist['CODELIST_ITEM_ORDER'] = ordr_range
combined_codelist['LOAD_ID'] = dt
combined_std.to_csv("STANDARDS.csv", header=['DATASOURCE_ID','Global_DM_VERSION_ID','Global_DM_VERSION_ENV','TARGET_DOMAIN','SOURCE_DOMAIN','DOMAIN_LABEL','SOURCE_VARIABLE','RAVE_LABEL','TYPE','VARIABLE_LENGTH','CONTROL_TYPE','CODELIST_OID','TARGET_VARIABLE','MANDATORY','ORIGIN','LOAD_ID'], index=False)
combined_codelist.to_csv("CODELIST.csv", header=['DATASOURCE_ID','Global_DM_VERSION_ID', 'Global_DM_VERSION_ENV','CODELIST_OID','CODED_VALUE','LABEL','CODELIST_ITEM_ORDER', 'LOAD_ID'], index=False)