[英]Pandas: iteratively concatenate columns stored in a dictionary of dataframes
Suppose I have a dictionary of pandas
dataframes where the keys are 0, 1, 2, ..., 999
, and the values are dataframes like this ( test_df
): 假设我有一个
pandas
数据帧的字典,其中键为0, 1, 2, ..., 999
,值是这样的数据帧( test_df
):
A B C
0 1.438161 -0.210454 -1.983704
1 -0.283780 -0.371773 0.017580
2 0.552564 -0.610548 0.257276
3 1.931332 0.649179 -1.349062
4 1.656010 -1.373263 1.333079
5 0.944862 -0.657849 1.526811
Say that the index means nothing to you, and that you want to create a new dataframe where columns A
and B
are concatenated: 假设索引对您没有任何意义,并且您想创建一个新数据框,其中
A
和B
列串联在一起:
mydf=pd.concat([test_df[0]['A'],test_df[0]['B']], axis=1, keys=['A','B'])
Now, can I use this line inside a for loop which iterates over all the keys in my dictionary of dataframes? 现在, 我可以在for循环内使用此行,该循环遍历数据帧字典中的所有键吗?
If not, what would be another way of doing this? 如果没有,那将是另一种方式呢? The result would be a dataframe with two columns,
A
and B
, and 6x1000
rows. 结果将是一个具有两列
A
和B
以及6x1000
行的数据帧。 The index column would therefore go from 0
to 5999
. 因此,索引列将从
0
到5999
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.