gpt4 book ai didi

Python - 将列附加到 DataFrame 列表

转载 作者:太空宇宙 更新时间:2023-11-04 00:20:37 26 4
gpt4 key购买 nike

我有一个由 DataFrame 组成的列表,我想在其中迭代 DataFrame 列表并根据数组向每个 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 循环的问题是它首先应用最后一个日期,然后它不将第二个日期应用到第二个 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)

最佳答案

将您的 for 循环更改为

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

关于Python - 将列附加到 DataFrame 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49212558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com