Assign Unique Values according Distinct Columns Values
<p>I know the question name is a little ambiguous.</p>

<p>My goal is to assign global key column based on 2 columns + unique value in my data frame.</p>

<p>For example</p>

<pre><code>CountryCode | Accident
   AFG          Car
   AFG          Bike
   AFG          Car
   AFG          Plane
   USA          Car
   USA          Bike
   UK           Car
</code></pre>

<p>Let Car = 01, Bike = 02, Plane = 03</p>

<p>My desire global key format is [Accident][CountryCode][UniqueValue]</p>

<p>Unique value is a count of similar [Accident][CountryCode]</p>

<p>so if Accident = Car and CountryCode = AFG and it is the first occurrence, the global key would be 01AFG01</p>

<p>The desired dataframe would look like this:</p>

<pre><code>CountryCode | Accident | GlobalKey
   AFG          Car        01AFG01
   AFG          Bike       02AFG01
   AFG          Car        01AFG02
   AFG          Plane      01AFG03
   USA          Car        01USA01
   USA          Bike       01USA02
   UK           Car        01UK01
</code></pre>

<p>I have tried running a for loop to append Accident Number and CountryCode together</p>

<p>for example:</p>

<pre><code>globalKey = []

for x in range(0,6):
    string = df.iloc[x, 1]
    string2 = df.iloc[x, 2]
    if string2 == 'Car':
        number = '01'
    elif string2 == 'Bike':
        number = '02'
    elif string2 == 'Plane':
        number = '03'
    #Concat the number of accident and Country Code
    subKey = number + string
    #Append to the list
    globalKey.append(subKey)
</code></pre>

<p>This code will provide me with something like <code>01AFG</code>, <code>02AFG</code> based on the value I assign. but I want to assign a unique value by counting the occurrence of when <code>CountryCode</code> and <code>Accident</code> is similar.</p>

<p>I am stuck with the code above. I think there should be a better way to do it using map function in Pandas.</p>

<p>Thanks for helping guys!
much appreciate!</p>

<p>You can try with <code>cumcount</code> to achieve this in a number of steps, like this:</p>

<pre><code>In [1]: df = pd.DataFrame({'Country':['AFG','AFG','AFG','AFG','USA','USA','UK'], 'Accident':['Car','Bike','Car','Plane','Car','Bike','Car']})

In [2]: df
Out[2]: 
  Accident Country
0      Car     AFG
1     Bike     AFG
2      Car     AFG
3    Plane     AFG
4      Car     USA
5     Bike     USA
6      Car      UK

## Create a column to keep incremental values for `Country`
In [3]: df['cumcount'] = df.groupby('Country').cumcount()

In [4]: df
Out[4]: 
  Accident Country  cumcount
0      Car     AFG         0
1     Bike     AFG         1
2      Car     AFG         2
3    Plane     AFG         3
4      Car     USA         0
5     Bike     USA         1
6      Car      UK         0

## Create a column to keep incremental values for combination of `Country`,`Accident`
In [5]: df['cumcount_type'] = df.groupby(['Country','Accident']).cumcount()

In [6]: df
Out[6]: 
  Accident Country  cumcount  cumcount_type
0      Car     AFG         0              0
1     Bike     AFG         1              0
2      Car     AFG         2              1
3    Plane     AFG         3              0
4      Car     USA         0              0
5     Bike     USA         1              0
6      Car      UK         0              0
</code></pre>

<p>And from that point on you can concatenate the values of <code>cumcount</code>, <code>cumcount_type</code> and <code>Country</code> to achieve what you're after.</p>

<p>Maybe you want to add <code>1</code> to each of the values you have under the different counts, depending on whether you want to start counting from 0 or 1.</p>

<p>I hope this helps.</p>

python pandas dataframe group-by