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

Time:09-20

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

CodePudding user response:

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

CodePudding user response:

I had to put [] :

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