Filling empty python dataframe using loops

后端 未结 2 1120
终归单人心
终归单人心 2021-01-05 06:29

Lets say I want to create and fill an empty dataframe with values from a loop.

import pandas as pd
import numpy as np

years = [2013, 2014, 2015]
dn=pd.Data         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-05 07:20

    As far as I know you should avoid to add line by line to the dataframe due to speed issue

    What I usually do is:

    l1 = []
    l2 = []
    
    for i in range(n):
       compute value v1
       compute value v2
       l1.append(v1)
       l2.append(v2)
    
    d = pd.DataFrame()
    d['l1'] = l1
    d['l2'] = l2
    

提交回复
热议问题