4

I have a pandas data frame known as "df":

  x y
0 1 2
1 2 4
2 3 8

I am splitting it up into two frames, and then trying to merge back together:

df_1 = df[df['x']==1]  
df_2 = df[df['x']!=1] 

My goal is to get it back in the same order, but when I concat, I am getting the following:

frames = [df_1, df_2]
solution = pd.concat(frames)
solution.sort_values(by='x', inplace=False)

  x y
1 2 4
2 3 8
0 1 2

The problem is I need the 'x' values to go back into the new dataframe in the same order that I extracted. Is there a solution?

0

4 Answers 4

3

use .loc to specify the order you want. Choose the original index.

solution.loc[df.index]

Or, if you trust the index values in each component, then

solution.sort_index()

enter image description here

setup

df = pd.DataFrame([[1, 2], [2, 4], [3, 8]], columns=['x', 'y'])

df_1 = df[df['x']==1]  
df_2 = df[df['x']!=1] 

frames = [df_1, df_2]
solution = pd.concat(frames)
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

In [14]: pd.concat([df_1, df_2.sort_values('y')])
Out[14]:
   x  y
0  1  2
1  2  4
2  3  8

Comments

0

When you are sorting the solution using solution.sort_values(by='x', inplace=False) you need to specify inplace = True. That would take care of it.

Comments

0

Based on these assumptions on df:

  1. Columns x and y are note necessarily ordered.
  2. The index is ordered.

Just order your result by index:

df = pd.DataFrame({'x': [1, 2, 3], 'y': [2, 4, 8]})
df_1 = df[df['x']==1]  
df_2 = df[df['x']!=1] 
frames = [df_2, df_1]
solution = pd.concat(frames).sort_index()

Now, solution looks like this:

   x  y
0  1  2
1  2  4
2  3  8

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.