You need to set the the variable data
equal to the appended data frame. Unlike the append
method on a python list the pandas append
does not happen in place
import pandas as pd
import numpy as np
data = pd.DataFrame([])
for i in np.arange(0, 4):
if i % 2 == 0:
data = data.append(pd.DataFrame({'A': i, 'B': i + 1}, index=[0]), ignore_index=True)
else:
data = data.append(pd.DataFrame({'A': i}, index=[0]), ignore_index=True)
print(data.head())
A B
0 0 1.0
1 2 3.0
2 3 NaN
NOTE: This answer aims to answer the question as it was posed. It is not however the optimal strategy for combining large numbers of dataframes. For a more optimal solution have a look at Alexander's answer below
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…