Search code examples
pythonpandasswap

switch rows between 2 dataframes


I have 2 dataframes looking like this :

df1 = pd.DataFrame({'col1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
       'col2': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
       'col3': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
       'cond': [5000, 6000, 6001, 5000, 6002, 6003, 5000, 6004, 6005, 5001]})
df2 = pd.DataFrame({'col1': [10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
       'col2': [10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
       'col3': [10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
       'cond': [6000, 5000, 5001, 6000, 5002, 5003, 6000, 5004, 5005, 6001]})

I need to swap rows between the two dataframes based on the condition column, the values which are 5000±100 should be in one dataframe and the values 6000±100 in another:

df_expected1 = pd.DataFrame({'col1': [1, 9, 8, 4, 6, 5, 7, 3, 2, 10],
                'col2': [1, 9, 8, 4, 6, 5, 7, 3, 2, 10],
                'col2': [1, 9, 8, 4, 6, 5, 7, 3, 2, 10],
                'cond': [5000, 5000, 5001, 5000, 5002, 5003, 5000, 5004, 5005, 5001]})

df_expected2 = pd.DataFrame({'col1': [10, 2, 3, 7, 5, 6, 4, 8, 9, 1],
                'col2': [10, 2, 3, 7, 5, 6, 4, 8, 9, 1],
                'col2': [10, 2, 3, 7, 5, 6, 4, 8, 9, 1],
                'cond': [6000, 6000, 6001, 6000, 6002, 6003, 6000, 6004, 6005, 6001]})

Solution

  • First concatenate the two dataframes and then split them based on column cond:

    df = pd.concat([df1, df2]).sort_index()
    
    df_expected1 = df[df.cond < 5500]
    df_expected2 = df[df.cond > 5500]
    

    I assumed here that df1 and df2 are dataframes. Value 5500 is arbitrarily chosen, maybe you need to change this.

    If the output needs to be a dictionary, you can use .to_dict().