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

前端 未结 5 1572
日久生厌
日久生厌 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:12

    There are 2 reasons you may append rows in a loop, 1. add to an existing df, and 2. create a new df.

    to create a new df, I think its well documented that you should either create your data as a list and then create the data frame:

    cols = ['c1', 'c2', 'c3']
    lst = []
    for a in range(2):
        lst.append([1, 2, 3])
    df1 = pd.DataFrame(lst, columns=cols)
    df1
    Out[3]: 
       c1  c2  c3
    0   1   2   3
    1   1   2   3
    

    OR, Create the dataframe with an index and then add to it

    cols = ['c1', 'c2', 'c3']
    df2 = pd.DataFrame(columns=cols, index=range(2))
    for a in range(2):
        df2.loc[a].c1 = 4
        df2.loc[a].c2 = 5
        df2.loc[a].c3 = 6
    df2
    Out[4]: 
      c1 c2 c3
    0  4  5  6
    1  4  5  6
    

    If you want to add to an existing dataframe, you could use either method above and then append the df's together (with or without the index):

    df3 = df2.append(df1, ignore_index=True)
    df3
    Out[6]: 
      c1 c2 c3
    0  4  5  6
    1  4  5  6
    2  1  2  3
    3  1  2  3
    

    Or, you can also create a list of dictionary entries and append those as in the answer above.

    lst_dict = []
    for a in range(2):
        lst_dict.append({'c1':2, 'c2':2, 'c3': 3})
    df4 = df1.append(lst_dict)
    df4
    Out[7]: 
       c1  c2  c3
    0   1   2   3
    1   1   2   3
    0   2   2   3
    1   2   2   3
    

    Using the dict(zip(cols, vals)))

    lst_dict = []
    for a in range(2):
        vals = [7, 8, 9]
        lst_dict.append(dict(zip(cols, vals)))
    df5 = df1.append(lst_dict)
    

提交回复
热议问题