【发布时间】: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