As a part of my simplified example script, I am adding a new column "generation" to my dataframe with the value "millenial". When the dataframe is empty, the first option using df.loc throws a ValueError, but the second approach properly creates the column as expected. Can someone please explain why the second approach successfully creates the new column on an empty dataframe, but the first approach throws a ValueError?
I also understand that I can check if a dataframe is empty prior to creating the new column, but this question stems mainly out of curiosity.
import pandas as pd
df = pd.DataFrame(columns=["name", "age"], data=None)
This results in a "ValueError: cannot set a frame with no defined index and a scalar"
df.loc[:, "generation"] = "millenial"
This successfully creates the "generation" column on an empty dataframe
df["generation"] = "millenial"