-1

I have a Pandas DataFrame which contain 30,000 row that looks something like:

   id Value
0   1   Ack
1   1   Ack
2   1  Auth
3   2  Auth
4   2  Auth
5   2   Ack
6   2   Ack

I want to convert this to something that looks like this:

id    Value      Value1  
1     'Ack'    'Ack1'
1     'Ack'    'Ack2
1     'Auth'    'Auth1'
2     'Auth'   'Auth1
2     'Auth'   'Auth2,
2     'Ack'    'Ack1'
2     'Ack'    'Ack2'

How can I solve it using Pandas?

w-m
  • 10,772
  • 1
  • 42
  • 49
  • 1
    Welcome to SO. Please finish the tour and you will understand [How ask a question](https://stackoverflow.com/help/asking) and modify question accordingly to a [Complete, and Verifiable working example](https://stackoverflow.com/help/mcve). Without posting your code you risk removal of your question. With your stated trail and error code... people are more willing to help you so we both can learn. Enjoy SO ;-) – ZF007 Apr 15 '19 at 14:27
  • [pandas example style for asking](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – ZF007 Apr 15 '19 at 14:29

1 Answers1

2

You could use cumcount to get an ascending number for each element in the id group, and append this to the Value string:

df["Value1"] = df.Value + (df.groupby(["id", "Value"]).cumcount() + 1).astype(str)
w-m
  • 10,772
  • 1
  • 42
  • 49
  • I have updated my question. how i can solve it now. – Mosiur Rahman Apr 17 '19 at 09:45
  • Review the [Pandas groupby documentation](https://pandas.pydata.org/pandas-docs/stable/user_guide/groupby.html), you could have worked this out yourself! I've updated the answer. – w-m Apr 17 '19 at 09:53