In python, while operating on list, we might need to store each loop output in a dataframe with each iteration.
So this recipe is a short example on how to append output of for loop in a pandas dataframe. Let's get started.
import pandas as pd
Let's pause and look at these imports. Pandas is generally used for data manipulation and analysis.
df= pd.DataFrame({'Table of 9': [9,18,27],
'Table of 10': [10,20,30]})
Let us create a dataframe containing some tables of 9 and 10.
for i in range(4,11):
df=df.append({'Table of 9':i*9,'Table of 10':i*10},ignore_index=True)
Comparing to append function in list, it applies a bit different for dataframe. As soon as any dataframe gets appnended using append function, it is note reflected in original dataframe. To store the appended information in a dataframe, we again assign it back to original dataframe.
print('df\n',df)
Simply use print function to print new appended dataframe.
Once we run the above code snippet, we will see:
Scroll down to the ipython notebook below to see the output.