Use groupby/cumcount
:
In [25]: df['C'] = df.groupby(['A','B']).cumcount()+1; df
Out[25]:
A B C
0 A a 1
1 A a 2
2 A b 1
3 B a 1
4 B a 2
5 B a 3
More Related Contents:
- Concatenate strings from several rows using Pandas groupby
- Pandas groupby with delimiter join
- Number rows within group in increasing order in a pandas dataframe
- Pandas rank by multiple columns
- How to group dataframe rows into list in pandas groupby
- Groupby value counts on the dataframe pandas
- Replace values in a pandas series via dictionary efficiently
- FutureWarning: elementwise comparison failed; returning scalar, but in the future will perform elementwise comparison
- Moving Average Pandas
- How to loop over grouped Pandas dataframe?
- How to deal with multi-level column names downloaded with yfinance
- How to use groupby to concatenate strings in python pandas?
- group by pandas dataframe and select latest in each group
- Pandas deleting row with df.drop doesn’t work
- How to melt 2 columns at the same time?
- Python Pandas Conditional Sum with Groupby
- Identifying statistical outliers with pandas: groupby and reduce rows into different dataframe
- Transform vs. aggregate in Pandas
- Sample each group after pandas groupby
- pandas.io.json.json_normalize with very nested json
- Updating a value in a pandas dataframe in an iterrows loop
- pandas groupby where you get the max of one column and the min of another column
- FutureWarning: The default value of regex will change from True to False in a future version
- Check if rows in one dataframe exist in another dataframe
- Pandas ParserError EOF character when reading multiple csv files to HDF5
- Python making combined bar and line plot with secondary y-axis
- Pandas groupby and aggregation output should include all the original columns (including the ones not aggregated on)
- How to convert string representation of dictionary in Pandas DataFrame to a new columns?
- Get the row corresponding to the max in pandas GroupBy [duplicate]
- When is it appropriate to use df.value_counts() vs df.groupby(‘…’).count()?