For循环进行计数并使用计数器结果创建一个新的数据框

时间:2019-05-08 11:50:47

标签: python dataframe for-loop counter

我正在尝试创建一个具有许多数据帧(每年数据)的计数器结果的新数据帧

我可以使用以下内容为计数器结果创建一个数据框

  df2014 = Counter(all_2014['outTrang']) # create variable for counter
    df2014 = pd.DataFrame.from_dict(df2014,orient = 'index').T # convert the variable into a df and transpose
    df2014['Year'] = [2014] # add year to the dataframe
    df2014 = df2014.rename(columns = {'index':'F', 0:'T'}) # rename the column names of the data frame
    df2015 = Counter(all_2015['outTrang'])
    df2015 = pd.DataFrame.from_dict(df2015,orient = 'index').T
    df2015['Year'] = [2015]
    df2015 = df2015.rename(columns = {'index':'F', 0:'T'})
    df2016 = Counter(all_2016['outTrang'])
    df2016 = pd.DataFrame.from_dict(df2016,orient = 'index').T
    df2016['Year'] = [2016]
    df2016 = df2016.rename(columns = {'index':'F', 0:'T'})
    df2017 = Counter(all_2017['outTrang'])
    df2017 = pd.DataFrame.from_dict(df2017,orient = 'index').T
    df2017['Year'] = [2017]
    df2017 = df2017.rename(columns = {'index':'F', 0:'T'})
    df2018 = Counter(all_2018['outTrang'])
    df2018 = pd.DataFrame.from_dict(df2018,orient = 'index').T
    df2018['Year'] = [2018]
    df2018 = df2018.rename(columns = {'index':'F', 0:'T'})

    dfAllYears = pd.concat([df2014,df2015,df2016,df2017,df2018],axis = 0, ignore_index = True) # combine all years to one dataframe
    dfAllYears = dfAllYears[['year','T','F']] # reorder the columns for the data frame 

但这看起来很长而且很手工。我试图创建一个for循环,但是它不起作用

    allyears = {'year':[2014,2015,2016,2017,2018],'df':[all_2014,all_2015,all_2016,all_2017,all_2018]}
    allYearFrame = pd.DataFrame(allyears)
    Topframe = pd.DataFrame()

    for y in allYearFrame.df:
      #global Topframe
      tempyear = Counter(allYearFrame['df']['outTrang'])
      tempframe = pd.DataFrame.from_dict(tempyear, orient = 'index').T
      tempframe['year'] = allYearFrame.loc[y.index,'year']
      tempframe = tempframe.rename(columns = {'index':'F', 0:'T'})
      Topframe  = pd.concat([tempframe,Topframe],axis = 0, ignore_index = True)

肯定有一种方法可以用更少的代码行做到这一点

0 个答案:

没有答案