简体   繁体   中英

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 ):

          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:

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?

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. The index column would therefore go from 0 to 5999 .

If df_dic is your dictionary, you can do:

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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