Skip to content

Right merge not preserve order #27453

Closed
@jesrael

Description

@jesrael

Base by SO question:

Merge with right join not preserve order of columns:

columns = ['A', 'B', 'C']
data_1 = [[2, 5, 3, 5], [8, 2, 4, 1], [6, 5, 9, 1]]
data_1 = np.array(data_1).T
df_1 = pd.DataFrame(data=data_1, columns=columns)
print (df_1)
   A  B  C
0  2  8  6
1  5  2  5
2  3  4  9
3  5  1  1

columns = ['A', 'B', 'C']
data_2 = [[2, 5, 3, 5], [7, 1, 3, 0], [np.nan, np.nan, np.nan, np.nan]]
data_2 = np.array(data_2).T
df_2 = pd.DataFrame(data=data_2, columns=columns)
print (df_2)
     A    B   C
0  2.0  7.0 NaN
1  5.0  1.0 NaN
2  3.0  3.0 NaN
3  5.0  0.0 NaN

#right join NOT preserve order
df1 = df_1.merge(df_2[['A', 'B']], on=['A', 'B'], how='right')
print (df1)
   A  B    C
0  5  1  1.0
1  2  7  NaN
2  3  3  NaN
3  5  0  NaN

Correct order for right join:

    A   B   C
0   2   7   NaN
1   5   1   1
2   3   3   NaN
3   5   0   NaN

#left join wpreserve order correct
df2 = df_1.merge(df_2[['A', 'B']], on=['A', 'B'], how='left')
print (df2)
   A  B  C
0  2  8  6
1  5  2  5
2  3  4  9
3  5  1  1

print (pd.show_versions())

INSTALLED VERSIONS
------------------
commit: None
python: 3.7.3.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 60 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en
LOCALE: None.None

pandas: 0.24.2
pytest: 4.3.1
pip: 19.0.3
setuptools: 40.8.0
Cython: 0.29.6
numpy: 1.16.2
scipy: 1.2.1
pyarrow: None
xarray: None
IPython: 7.4.0
sphinx: 1.8.5
patsy: 0.5.1
dateutil: 2.8.0
pytz: 2018.9
blosc: None
bottleneck: 1.2.1
tables: 3.5.1
numexpr: 2.6.9
feather: None
matplotlib: 3.0.3
openpyxl: 2.6.1
xlrd: 1.2.0
xlwt: 1.3.0
xlsxwriter: 1.1.5
lxml.etree: 4.3.2
bs4: 4.7.1
html5lib: 1.0.1
sqlalchemy: 1.3.1
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
gcsfs: None
None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions