【问题标题】:Python - Appending a column to a list of DataFramesPython - 将一列附加到 DataFrames 列表
【发布时间】:2018-08-19 03:14:14
【问题描述】:

我有一个由 DataFrames 组成的列表,我想在其中迭代 DataFrames 列表并根据数组向每个 DataFrame 插入一列。

以下是我为说明目的而创建的一个小示例。如果它只有 4 个 DataFrame,但我的数据集要大得多,我会手动执行此操作:

#Create dataframes
df1 = pd.DataFrame(list(range(0,10)))
df2 = pd.DataFrame(list(range(10,20)))
df3 = pd.DataFrame(list(range(20,30)))
df4 = pd.DataFrame(list(range(30,40)))

#Create list of Dataframes
listed_dfs = [df1,df2,df3,df4]

#Create list of dates
Dates = ['2015-05-15','2015-02-17', '2014-11-14', '2014-08-14']

#Objective: Sequentially append each instance of "Dates" to a new column in each dataframe
#First, create list of locations for iterations
locations = [0,1,2,3]

#Second, create for loop to iterate over [Need help here]
#Example: for the 1st Dataframe in the list of dataframes, add a column 'Date' that 
#         has the the 1st instance of the 'Dates' list for every row,
#         then for the 2nd DataFrame in the list of dataframes, add the 2nd instance of the 'Dates' list for every row
for i in Dates:
    for a in locations:
        listed_dfs[a]['Date'] = i

print(listed_dfs)

上述for循环的问题是它首先应用最后一个日期,然后它不将第二个日期应用到第二个DataFrame,只应用每个DataFrame的第一个日期。

for 循环的所需输出:

listed_dfs[0]['Date'] = Dates[0]
listed_dfs[1]['Date'] = Dates[1]
listed_dfs[2]['Date'] = Dates[2]
listed_dfs[3]['Date'] = Dates[3]

pd.concat(listed_dfs)

【问题讨论】:

    标签: python list pandas dataframe


    【解决方案1】:

    如果像您的示例一样,按顺序循环遍历数据帧,则可以zip 数据帧和日期如下。

    for df, date in zip(listed_dfs, Dates):
        df['Date'] = date
    

    这消除了对locations 列表的需要。

    【讨论】:

      【解决方案2】:

      如果我没有很好地理解它,那么问题是您在 Dates 的每次迭代中都覆盖了所有四个数据框中的“Date”列。一个解决方案可能只有一个这样的“for”循环:

      for a in locations:
          listed_dfs[a]['Date'] = Dates[a]
      

      【讨论】:

        【解决方案3】:

        按照你想要的输出:

        listed_dfs[0]['Date'] = Dates[0]
        listed_dfs[1]['Date'] = Dates[1]
        listed_dfs[2]['Date'] = Dates[2]
        listed_dfs[3]['Date'] = Dates[3]
        
        pd.concat(listed_dfs)
        

        请注意,一行的索引值是相同的,因此,0 和 0、1 和 1 等等。这基本上就是您所需要的。

        for i in range(len(Dates)):
            listed_dfs[i]['Date'] = Dates[i]
        
        pd.concat(listed_dfs)
        

        【讨论】:

          【解决方案4】:

          将你的 for 循环更改为

          for i,j in zip(Dates,locations):
                  listed_dfs[j]['Date'] = i
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-11-13
            • 1970-01-01
            • 1970-01-01
            • 2015-07-11
            • 2020-10-29
            • 1970-01-01
            • 2017-10-05
            相关资源
            最近更新 更多