0

I am using a for loop to merge csv files on jupyter notebook, however my result returns a list instead of a dataframe. Could someone help me and tell me what I am doing wrong? Thank you in advance.

files = ['babd_light_z1.csv','babd_light_z2.csv','babd_light_z3.csv']
data = []
for f in files:
     data.append(pd.read_csv(f))

type(data) # returns  list
1
  • 1
    Change data = [] to data = pd.DataFrame() and reassign data in the loop body. Commented May 17, 2020 at 8:56

2 Answers 2

1

You can simply use pd.concat(data, axis=0, ignore_index=True) outside your loop to merge your csv files as in:

files = ['babd_light_z1.csv', 'babd_light_z2.csv', 'babd_light_z3.csv']

data = []

for f in files:
    data.append(pd.read_csv(f))

df = pd.concat(data, axis=0, ignore_index=True)

type(df) should return pandas.core.frame.DataFrame

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

Comments

0

Give this a shot:

combined = pd.combine(data, axis=0)

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.