Akhil
Akhil

Reputation: 65

Merging excel files using pandas and adding a custom column gives error

I have a bunch of excel files which I am merging into a csv file. Once the files are merged, I need to add a few more columns in the beginning of the csv file (I am planning to populate those columns using parameters e.g. GLB_DM_VER to populate Global_DM_Version column).

The following script gives me an error:

AttributeError: 'NoneType' object has no attribute 'to_csv'

I am new to Python and would really appreciate any help on this issue.Thanks.

import glob

path= input("Enter the location of files ")

GLB_DM_VER = input("Enter global DM version")

file_list = glob.glob(path+"\*.xls")

excels = [pd.ExcelFile(name) for name in file_list] 

frames = [x.parse(x.sheet_names[2], header=0,index_col=None) for x in excels]

combined = pd.concat(frames)

combined = combined.insert(loc=1, column = 'Global_DM_Version', value = GLB_DM_VER )

combined.to_csv("STAND_2.csv", header=['TARGET_DOMAIN','SOURCE_DOMAIN','DOMAIN_LABEL','SOURCE_VARIABLE','RAVE_LABEL','TYPE','VARIABLE_LENGTH','CONTROL_TYPE','CODELIST_OID','TARGET_VARIABLE','MANDATORY','RAVE_ORIGIN'], index=False)

Upvotes: 0

Views: 58

Answers (1)

tobsecret
tobsecret

Reputation: 2522

It's the following line:

combined = combined.insert(loc=1, column = 'Global_DM_Version', value = GLB_DM_VER )

Just write this instead:

combined.insert(loc=1, column = 'Global_DM_Version', value = GLB_DM_VER)

Explanation: pd.DataFrame.insert does not return a DataFrame but modifies the DataFrame inplace. If a function does not return anything in python, it returns None instead, hence you see the error that you see.

Upvotes: 1

Related Questions