'python - merge function output comes out sorted
below are my codes
vals = [1,123,223,510,2356,7810]
id = ['AAA', 'AA', 'A', 'BBB', 'BB', 'B']
dd = pd.DataFrame(vals,index=id,columns=['Values'])
ddd = ["AAA","A", "AA", "A", "A", "BBB", "BBB", "A"]
ddd = pd.DataFrame(ddd,columns=['Values']).set_index('Values')
dout = pd.merge(ddd,dd,left_index=True,right_index=True,sort=False)
my output is sorted
Values
A 223
A 223
A 223
A 223
AA 123
AAA 1
BBB 510
BBB 510
whereas the sequence for dataframe ddd is ['AAA', 'AA', 'A', 'BBB', 'BB', 'B'], which is the output sequence I want i.e unsorted. I have added sort=False but that doesnt work. what am I missing?
Solution 1:[1]
the only way around the issue was adding these two lines at the bottom of my code and it gave me what I wanted
dout_new = dout.drop_duplicates().T
columns_ = ddd.reset_index().Values.to_list()
dout_new = dout_new[columns_].T
if anyone has a sleeker answer, let us know!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user13412850 |
