Python—将列附加到数据帧列表

Python—将列附加到数据帧列表,python,list,pandas,dataframe,Python,List,Pandas,Dataframe,我有一个由DataFrames组成的列表,我希望迭代DataFrames列表,并基于数组向每个DataFrame插入一列 下面是一个小例子,我已经创建了说明的目的。如果只有4个数据帧,但我的数据集要大得多,我会手动执行此操作: #Create dataframes df1 = pd.DataFrame(list(range(0,10))) df2 = pd.DataFrame(list(range(10,20))) df3 = pd.DataFrame(list(range(20,30))) d

我有一个由DataFrames组成的列表,我希望迭代DataFrames列表,并基于数组向每个DataFrame插入一列

下面是一个小例子,我已经创建了说明的目的。如果只有4个数据帧,但我的数据集要大得多,我会手动执行此操作:

#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循环的问题是,它首先应用最后一个日期,然后不将第二个日期应用于第二个数据帧,只将第一个日期应用于每个数据帧

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)

将for循环更改为

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

按照您想要的输出:

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)

如果我理解得很好,问题是在日期的每次迭代中,您都会覆盖所有四个数据帧中的“Date”列。一个解决方案可能只有一个“for”循环,如下所示:

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

如您的示例所示,如果按顺序循环数据帧,则可以
zip
dataframes和日期,如下所示

for df, date in zip(listed_dfs, Dates):
    df['Date'] = date
这样就不需要
位置
列表