简体   繁体   English

熊猫:以迭代方式串联存储在数据框字典中的列

[英]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: 假设索引对您没有任何意义,并且您想创建一个新数据框,其中AB列串联在一起:

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. 结果将是一个具有两列AB以及6x1000行的数据帧。 The index column would therefore go from 0 to 5999 . 因此,索引列将从05999

If df_dic is your dictionary, you can do: 如果df_dic是您的字典,则可以执行以下操作:

pd.concat([df[['A', 'B']] for df in df_dic.values()]).reset_index(drop=True)

Here is what the result looks like if df_dic contains two key-value pairs: 如果df_dic包含两个键值对,则结果如下df_dic

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM