I want to append a row to a pandas data frame without using df.loc
. I am looking for something similar to the .append()
method used in lists.
I currently do this, but I'm looking for a different solution:
stupid_table = pd.DataFrame(columns=['max_depth', 'features'])
for max_depth in np.arange(50, 101):
new_row = pd.DataFrame({'max_depth':[max_depth], 'features':[10]})
stupid_table = pd.concat([stupid_table, new_row], axis=0)
I would like to declare new_row
as a list or pandas Series rather than a pandas data frame. Is there a way to make the following syntax work?
stupid_table = pd.DataFrame(columns=['max_depth', 'features'])
for max_depth in np.arange(50, 101):
new_row = [max_depth, 10]
stupid_table = pd.concat([stupid_table, new_row], axis=0)
I specifically want to do this because the real data frame that I want to append new rows to has tons of columns and I am too lazy to write down the new row as a dictionary. I'd rather type the numbers in a list. I do this very often and a more convenient syntax would do wonders for my mental health.
EDIT: I prefer not using .loc
because my for loop is taking each value in the numpy arrays. Switching the current syntax to for value in range(len(np.arange(start, stop)))
to make the for loop to use indexes, then I'll have to translate the combinations into numeric values because, in the real example, I have tons of combinations. I'd rather not do that.