0

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)

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.