Reputation: 791
I have 2 data frames with one column each. Index of the first is [C,B,F,A,Z]
not sorted in any way. Index of the second is [C,B,Z]
, also unsorted.
I use pd.concat([df1,df2],axis=1)
and get a data frame with 2 columns and NaN in the second column where there is no appropriate value for the index.
The problem I have is that index automatically becomes sorted in alphabetical order.
I have tried = pd.concat([df1,df2],axis=1, names = my_list)
where my_list = [C,B,F,A,Z]
, but that didn't make any changes.
How can I specify index to be not sorted?
Upvotes: 2
Views: 6531
Reputation: 394179
This seems to be by design, the only thing I'd suggest is to call reindex
on the concatenated df and pass the index of df
:
In [56]:
df = pd.DataFrame(index=['C','B','F','A','Z'], data={'a':np.arange(5)})
df
Out[56]:
a
C 0
B 1
F 2
A 3
Z 4
In [58]:
df1 = pd.DataFrame(index=['C','B','Z'], data={'b':np.random.randn(3)})
df1
Out[58]:
b
C -0.146799
B -0.227027
Z -0.429725
In [67]:
pd.concat([df,df1],axis=1).reindex(df.index)
Out[67]:
a b
C 0 -0.146799
B 1 -0.227027
F 2 NaN
A 3 NaN
Z 4 -0.429725
Upvotes: 5