I am appending rows to a pandas DataFrame within a for loop, but at the end the dataframe is always empty. I don't want to add the rows to an array and then call the DataFrame constructer, because my actual for loop handles lots of data. I also tried pd.concat
without success. Could anyone highlight what I am missing to make the append statement work? Here's a dummy example:
import pandas as pd
import numpy as np
data = pd.DataFrame([])
for i in np.arange(0, 4):
if i % 2 == 0:
data.append(pd.DataFrame({'A': i, 'B': i + 1}, index=[0]), ignore_index=True)
else:
data.append(pd.DataFrame({'A': i}, index=[0]), ignore_index=True)
print data.head()
Empty DataFrame
Columns: []
Index: []
[Finished in 0.676s]