Rahul Agarwal
Rahul Agarwal

Reputation: 4100

Replace all values of row with Frequent value of a row

I have a df like this:

Index  Parameters   A    B   C   D   E
1      Apple        1    2   3   4   5
2      Banana       2    4   5   3   5
3      Potato       3    5   3   2   1
4      Tomato       1    1   1   1   1
5      Pear         $4    $5   $5   $5   $3

Problem statement:

Based on the value of Parameters column and row Pear I want to get the most frequent value and replace with all the values

Output df:

Index  Parameters   A    B   C   D   E
1      Apple        1    2   3   4   5
2      Banana       2    4   5   3   5
3      Potato       3    5   3   2   1
4      Tomato       1    1   1   1   1
5      Pear         $5    $5   $5   $5   $5

My Code:

df_transposed = df.set_index("Parameters").T.rename_axis('Fruits').reset_index()
df_transposed["Pear"] = df_transposed.Pear.mode()
df = df_transposed.set_index("Fruits").T.rename_axis('Parameters').reset_index()

I have a solution which works perfectly well with transposing the df and using mode on it and then re-transpose the data.

The point is it is too many steps. Just wondering if it can be done row wise also

Upvotes: 1

Views: 43

Answers (1)

Andy L.
Andy L.

Reputation: 25259

Try slicing and df.mode. As you said you have so many columns to replace, you may filter columns base on columns NOT getting replaced.

cols = df.columns.drop(['Index', 'Parameters'])

df.loc[df.Parameters.eq('Pear'), cols] = df.loc[df.Parameters.eq('Pear'), cols].mode(axis=1)[0]


Out[77]:
   Index Parameters   A   B   C   D   E
0      1      Apple   1   2   3   4   5
1      2     Banana   2   4   5   3   5
2      3     Potato   3   5   3   2   1
3      4     Tomato   1   1   1   1   1
4      5       Pear  $5  $5  $5  $5  $5

Upvotes: 1

Related Questions