I am generating a lot of correlation dataframes in Pandas. They all have the same shape and format. So here is dataframe A:-
free_memory memory_in_use active_memory
free_memory 1 0.190912742 0.375301656
memory_in_use 0.190912742 1 0.962653384
active_memory 0.375301656 0.962653384 1
and this is dataframe B:-
free_memory memory_in_use active_memory
free_memory 1 0.673434243 0.712713697
memory_in_use 0.673434243 1 0.991687459
active_memory 0.712713697 0.991687459 1
When I get a dataframe C such that all of its cells are mean of corresponding cells in A and B, Pandas mixes up the order of the rows. For example the result C looks like this:-
free memory memory in use active memory
active memory 0.296996 0.960049 1.000000
free memory 1.000000 0.520667 0.296996
memory in use 0.520667 1.000000 0.960049
As you can see, column order is preserved but row order is effed up and I dont get the nice diagonal 1's. How can I keep the order same as in the input dataframes? The relevant code that is doing this is:-
df_concat.groupby(df_concat.index).mean()
You can add parameter sort=False
to function groupby
:
print df_concat.groupby(df_concat.index, sort=False).mean()
free_memory memory_in_use active_memory
free_memory 1.000000 0.190913 0.375302
memory_in_use 0.190913 1.000000 0.962653
active_memory 0.375302 0.962653 1.000000
Docs :
sort : boolean , default True
Sort group keys. Get better performance by turning this off. Note this does not influence the order of observations within each group. groupby preserves the order of rows within each group.
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.