Member-only story
Automating Pandas DataFrame Column Creation in Python
Let’s see how one can create columns using a loop.
Very often we happen to want to add a column to our dataframe that has values based on those of other columns. Usually, this new column needs to perform a summarizing function, putting the data in order.
Of course, we can create such a column with the help of the pandas library as we have already seen in this article. However, there is a simpler, faster, and more elegant way: just use a “ for ” loop.
What is a “for” loop?
A “for” loop is an iteration method that allows us to iterate through a sequence(more or less in technical terms). Since the iteration is closely related to this sequence of objects, the “for” loop creates a finite and consequently a priori determined loop.
for itarator_variable in sequence_name:Statements. . .Statements
How to use a “for” loop to create a column in pandas
As already mentioned, the peculiarities of the “for” loop allow us to add a new column to our dataframe and assign its values in a clear, simple, and elegant way.