Cumulative counts of items in a Pandas dataframe i'm using a dataframe full of headers/field names to develop a schema and I need unique values for each header - but each header should be represented. I want to loop though the dataframe list of fields and the second time I come across a field name, I want to add an ever increasing number to it. The order is important.

e.g.

headers['name'] =pd.DataFrame['vegetable', 'fruit', 'meat', 'dairy', 'meat', 'fruit', 'fruit']


I need the output to be: 

name      | name+count
vegetable | vegetable
fruit     | fruit
meat      | meat
dairy     | dairy
meat      | meat1
fruit     | fruit1
fruit     | fruit2




for header in headers['names']:
       row = headers.loc[headers['names'] == header].index
       if len(row) &gt; 1:
           for i in range(2, len(row)):
               headers['name+count'][row[i]] = headers['names'][row[i]] + str(i-1)
               print(headers['name+count'][row[i]])
You can group by the name and then append an increasing number to it:

import pandas as pd

def add_count(x):
    return x + ([""] + list(map(str, range(1, len(x)))))

df = pd.DataFrame(['vegetable', 'fruit', 'meat', 'dairy', 'meat', 'fruit', 'fruit'],
                  columns=["name"])
x = df.groupby("name", as_index=False)["name"].apply(add_count)
df["name2"] = x.reset_index(level=0, drop=True)
print(df)
#         name       name2
# 0  vegetable   vegetable
# 1      fruit       fruit
# 2       meat        meat
# 3      dairy       dairy
# 4       meat       meat1
# 5      fruit      fruit1
# 6      fruit      fruit2


This avoids manually iterating over rows or columns, something that is usually a good thing when dealing with pandas dataframes.