Creating an empty list and adding elements to it


I want to make an empty list or vector or dataframe and add elements which are of tyep String31 to it. But trying the dataframe option and push! I face this error:

" ArgumentError: it is not allowed to insert collections of type String31 into a DataFrame. Only Tuple, AbstractArray, AbstractDict, DataFrameRow
and NamedTuple are allowed."
I appreciate any help!

The error message tells you what’s wrong: push! needs one of the specified collection types when used with a DataFrame. This is because you can only push rows to the DataFrame, not single values (which wouldn’t make much sense as all columns have to have the same length).

If your DataFrame has only one column, try replacing push!(df, x) with push!(df, (x,)) which creates a Tuple from your value to add it to the DataFrame. If you have more than one column, you need to provide a value for each column, ie push!(df, (x, y, z))

Thanks a lot!
I understood that I should transform it into a tuple or other things mentioned in the error but I didn’t know how! (x,) was the key
If I want to add several items using append!, What should I do?

If you use append! it’s probably best to just create a DataFrame out of the items you want to append (if you have the columns as vectors)