简体   繁体   中英

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

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

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]})])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM