Skip to content Skip to sidebar Skip to footer

Appending Dataframe To Empty Dataframe Is Not Working

I declared an empty data frame near the top of my file with a global scope: final_df = pd.DataFrame() I have stats_dfsuccessfully printing the correct value, but final_df is not

Solution 1:

You need assign to new DataFrame, because use DataFrame.append, not pure python append:

stats_feature_names = ['a','b']
final_df = pd.DataFrame()

X = [[1,2]]
stats_df = pd.DataFrame(X, columns=stats_feature_names).sum().to_frame().T
print('statsdf being appended: \n', stats_df)
print('final_df before append: \n', final_df)
final_df = final_df.append(stats_df, ignore_index=True)
print('final_df after append: \n', final_df)
    a  b
0  1  2

But better solution is append to list (pure python append) and out of loop use concat:

L = []
for x in iterator:
    stats_df = pd.DataFrame([[1,2]], columns=stats_feature_names).sum().to_frame().T
    L.append(stats_df)

final_df = pd.concat(L, ignore_index=True)
print('final_df after append: \n', final_df)

Post a Comment for "Appending Dataframe To Empty Dataframe Is Not Working"