How to append rows in a pandas dataframe in a for loop?

前端 未结 5 1569
日久生厌
日久生厌 2020-11-29 16:43

I have the following for loop:

for i in links:
     data = urllib2.urlopen(str(i)).read()
     data = json.loads(data)
     data = pd.DataFrame(data.items())         


        
5条回答
  •  时光说笑
    2020-11-29 17:00

    I have created a data frame in a for loop with the help of a temporary empty data frame. Because for every iteration of for loop, a new data frame will be created thereby overwriting the contents of previous iteration.

    Hence I need to move the contents of the data frame to the empty data frame that was created already. It's as simple as that. We just need to use .append function as shown below :

    temp_df = pd.DataFrame() #Temporary empty dataframe
    for sent in Sentences:
        New_df = pd.DataFrame({'words': sent.words}) #Creates a new dataframe and contains tokenized words of input sentences
        temp_df = temp_df.append(New_df, ignore_index=True) #Moving the contents of newly created dataframe to the temporary dataframe
    

    Outside the for loop, you can copy the contents of the temporary data frame into the master data frame and then delete the temporary data frame if you don't need it

提交回复
热议问题