0

I have a DataFrame A

     DataFrame A
| I1  | I2 |  Rank  |
| -------- |--------|

| 1   | a  |   1    |
| 1   | b  |   2    |

| 2   | a  |   1    |
| 2   | b  |   2    |
| 2   | c  |   3    |

and I have a maximum value of rank for each I1 found by z.groupby('I1').max()

    max A
| I1  |  Rank  |
| --- |--------|

| 1   |   2    |
| 2   |   3    |

now I want to change my dataframe base on that 2 input and make the rest NaN

     Desire Output
| I1  | I2 |  Rank  |
| -------- |--------|

| 1   | a  |  NaN   |
| 1   | b  |  num1  |

| 2   | a  |  NaN   |
| 2   | b  |  NaN   |
| 2   | c  |  num2  |

How do I achieve that?

PrinceZard
  • 25
  • 4
  • Why you have a [multi-index](https://stackoverflow.com/questions/tagged/multi-index) tag in your question/title ? Can you make an example with the `pd.DataFrame` constructor ? [pandas-minimal-reproducible-example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – Timeless Jun 19 '23 at 11:08

1 Answers1

0

IIUC, you can use :

m = df.groupby("I1")["Rank"].transform("max").ne(df["Rank"])

df["Rank"] = df["Rank"].mask(m, other=np.nan) # `np.nan` by default

Output :

print(df)

   I1 I2  Rank
0   1  a   NaN
1   1  b  2.00
2   2  a   NaN
3   2  b   NaN
4   2  c  3.00

Input used :

df = pd.DataFrame(
    {'I1': [1, 1, 2, 2, 2],
     'I2': ['a', 'b', 'a', 'b', 'c'],
     'Rank': [np.nan, 2.0, np.nan, np.nan, 3.0]}
)
Timeless
  • 22,580
  • 4
  • 12
  • 30
  • What if the condition is something like >= 2? – PrinceZard Jun 19 '23 at 11:24
  • You need to be more specific. – Timeless Jun 19 '23 at 11:24
  • Keep the rank that is >= 2 instead of max rank for each I1 and change it to some arbitrary number – PrinceZard Jun 19 '23 at 11:29
  • That's not what you're asking for in your question. You should update the given example and make sure to update the expected output as well. Or, to avoid moving targets, you should [open](https://stackoverflow.com/questions/ask) a new *question*. – Timeless Jun 19 '23 at 11:32