How can I replace .append with pd.concat in a loop?

Viewed 38

I wonder how to replace the .append in a loop.

for i in range(0,len(DF)):
        result = result.append({'Test': DF.iloc[i].name},ignore_index=True)

I know how to use CONCAT but I don't understand how to do it in a loop.

Using Concat :

for i in range(0,len(DF)):
        result = pd.concat([result,pd.DataFrame({'Test': DF.iloc[i].name})])

Throw :

ValueError: If using all scalar values, you must pass an index

2 Answers
result = pd.DataFrame()
for i in range(0,len(DF)):
        result = pd.concat([result, {'Test': DF.iloc[i].name})

I had to put [] :

for i in range(0,len(DF)):
        result = pd.concat([result,pd.DataFrame({'Test': [DF.iloc[i].name]})])
Related