data-monkey
data-monkey

Reputation: 1705

Combining results from a loop into a DataFrame

Working with Python Pandas 0.19.1.

I'm calling a function in loops, which returns a numeric list with length of 4 each time. What's the easiest way of concatenating them into a DataFrame?

I'm doing this:

result = pd.DataFrame()
for t in dates:
    result_t = do_some_stuff(t)
    result.append(result_t, ignore_index=True)

The problem is that it concatenates along the column instead of by rows. If dates has a length of 250, it gives a single-column df with 1000 rows. Instead, what I want is a 250 x 4 df.

Upvotes: 1

Views: 10503

Answers (1)

jezrael
jezrael

Reputation: 862601

I think you need append all DataFrames to list result and then use concat:

result = []
for t in dates:
    result.append(do_some_stuff(t))

print (pd.concat(result, axis=1))    

Upvotes: 4

Related Questions