我有一个由DataFrames组成的列表,我想迭代DataFrames列表,并根据数组向每个DataFrame插入一列。
以下是我为说明目的而创建的一个小示例。我会手动执行此操作,如果它只有4个DataFrames但我的数据集要大得多:
singletons
上述for循环的问题在于它首先应用了最后一个日期,然后它没有将第二个日期应用于第二个DataFrame,只是每个DataFrame的第一个日期。
来自for循环的所需输出:
#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)
答案 0 :(得分:2)
将for循环更改为
filter(lambda x: x%2!=0, mylist)
答案 1 :(得分:1)
使用您想要的输出:
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)
答案 2 :(得分:0)
如果我感觉不好,问题是您在Dates的每次迭代中覆盖了所有四个数据帧中的“Date”列。解决方案可能只是一个'for'循环:
for a in locations:
listed_dfs[a]['Date'] = Dates[a]
答案 3 :(得分:0)
如果您在示例中顺序遍历数据框,则可以while ((line = file.readLine()) != null) {
ArrayList<String> arr2= new ArrayList<String>();
String[] words = line.split("\\,");
for (int ae = 0; ae < words.length; ae++) {
arr2.add(words[ae]);
}
arr1.add(arr2);
}
file.close();
数据框和日期如下所示。
zip
这消除了对for df, date in zip(listed_dfs, Dates):
df['Date'] = date
列表的需求。