1

I am trying to merge (concat) dataframes selected from csv files. I have previously achieve this manually but want to use a (tkinter) filedialog so that the user can select one or more files to use in the dataframe. What I have is:

  1. Read the filenames into an array until the user presses cancel - all good
while True:
    root = Tk()
    root.withdraw()
    filename = filedialog.askopenfilename(title='Open data file', filetypes=(("Comma separated values", "*.csv"),))
    if filename =="":
        break
    filenames.append(filename)
  1. Use the filenames to generate a dataframe - all good individually (I think, as they print OK)
def read_Yokogawa(filename):
    frame = pd.read_csv(filename, header = 46, skiprows = [47, 48], low_memory = False, parse_dates = True, infer_datetime_format = True, na_values = ['+OVER', '-OVER'])
    print(frame)
    return frame

dfs=[]  #Set an empty array of dataframes
for filename in filenames:
    dfs.append([read_Yokogawa(filename)])   #Append each dataframe to the array
  1. Concat the array of dataframes together - not good
df = pd.concat(dfs)

I get the error: TypeError: cannot concatenate object of type ''; only Series and DataFrame objs are valid

All the files have the same columns as they are generated by the same software. I have also tried pd.concat([dfs]) but get the same error.

3
  • It might help to include sample data and type of, for example, dfs[0] and dfs[1], or [i for i in dfs if not isinstance(i, pd.DataFrame)] Commented Feb 26, 2020 at 20:50
  • 1
    Change dfs.append([read_Yokogawa(filename)]) to dfs.append(read_Yokogawa(filename)). Commented Feb 26, 2020 at 20:51
  • 1
    I think your problem come from the square brackets. Try: dfs.append(read_Yokogawa(filename)) Commented Feb 26, 2020 at 20:54

1 Answer 1

1

Both @Quang and @Renaud suggested the same thing, which turned out to be a good answer to the question.

dfs=[]
for filename in filenames:
    dfs.append(read_Yokogawa(filename))
df = pd.concat(dfs)
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.