An empty pandas DataFrame is created:
results = pd.DataFrame(columns=['age','timestamp','score']).set_index(['age', 'timestamp'])
and more DataFrames will be appended to the initial results
DataFrame.
result = pd.DataFrame({'age': age,
'timestamp': timestamp,
'score': score
}).set_index(['age', 'timestamp'])
# error then occurs at this point
results.append(result)
and we get the error
ValueError: If using all scalar values, you must pass an index
Whats the proper way to append the second DataFrame?
Try this. Since your newly added record only have one row. Initializing it via a new dataframe introduces overheads. Just pass the dict to the current df via
.loc
would work in your case.Note that adding records one by one is not performance-efficient. But if this is part of your code logic which is unavoidable, then
.loc
will give you performance far better thanpd.append()
orpd.concat()
.