Home > front end >  Interchange values of two rows in pandas for selected columns in order of row and column:
Interchange values of two rows in pandas for selected columns in order of row and column:

Time:03-30

I've been trying this for a while now, but I can't seem to get the results. After searching a lot (including stackoverflow) I coudn't find any solutions:

Here's the data:

k   l   x1  x2  x3  x4  x5
k_1 0   1   2   3   8   4
k_2 1   1   2   5   8   6

This is the desired result:

k   l   x1  x2  x3  x4  x5
k_1 0   1   2   3   8   4
k_2 1   1   2   5   8   6
k_1 0   1   2   5   8   4
k_2 1   1   2   3   8   6
k_1 0   1   2   3   8   6
k_2 1   1   2   5   8   4

So far this is the closest I could get:

k   l   x1  x2  x3  x4  x5
k_1 0   1   2   3   8   4
k_2 1   1   2   5   8   6
k_1 0   1   2   5   8   6
k_2 1   1   2   3   8   4
k_1 0   1   2   3   8   4
k_2 1   1   2   5   8   6

This is what I tried:

df = pd.DataFrame([['k_1', 0, 1, 2, 3, 8, 4], ['k_2', 1, 1, 2, 5, 8, 6]],
                  columns = ['k', 'l', 'x1', 'x2', 'x3', 'x4', 'x5'])

def get_ds(df):
    arr = np.where(df.iloc[0, :] != df.iloc[1, :])
    ind = [i for i in arr[0] if i not in [0, 1]]
    return ind

def intchng_df(df):
    temp_df = pd.concat([df] * (len(get_ds(df))   1), ignore_index=True)
    return temp_dfI

temp_df = intchng_df(df)

i = 2
for col, row in temp_df.iteritems():
    if col in [temp_df.columns[c] for c in get_ds(df)]:
        t = temp_df.loc[i, col]
        temp_df.loc[i, col] = temp_df.loc[i 1, col]
        temp_df.loc[i 1, col] = t

temp_df

Basically, What I'm trying to achieve:

  • Find Columns which have different values except k & l -> x3 & x5 in this case using get_ds()

  • Find all possible combination of row values for these two columns -> using intchng_df().

  • Leave the original two rows intact and interchange remaining row values for each column one by one.

As given in the desired result, for x3 row2 and row3 were interchanged, and then for x5 row 4 and row 5 were interchanged.

CodePudding user response:

Here you go

unique_entries = df.filter(like='x').apply(lambda x: x.duplicated(keep=False).all())

permutation_df = df.copy()
for col in unique_entries[~unique_entries].index:
    (df.loc['k1',col],df.loc['k2',col]) = (df.loc['k2',col],df.loc['k1',col])
    permutation_df = pd.concat([permutation_df,df])
    (df.loc['k1',col],df.loc['k2',col]) = (df.loc['k2',col],df.loc['k1',col])

note that in Python you can swap using tuples which makes it more compact.

  • Related