0

I have a col called id in a dataframe called _newdata which looks like this. Note that this is a part of the values in the column and not the entire thing.

1
1
1
2
2
2
2
2
4
4
4
4
4
5
5
5
5
7
7
7
7
7
8
8
8
8
10
10
10

What I want to do is the make rename the 'id' with values so that it is in running numbers. Which means I want it to look like this

1
1
1
2
2
2
2
2
3
3
3
3
3
4
4
4
4
5
5
5
5
5
6
6
6
6
7
7
7

I tried using this but it didn't seem to do anything to the file. Could someone tell me where I went wrong or suggest a method to do what I want it to do?

count = 1 #values start at 1
    for i, row in _newdata.iterrows():
        if row['id']==count or row['id']==count+1:
            pass

        else:
            count+=1
            row['id']=count
Hari Krishnan
  • 2,049
  • 2
  • 18
  • 29
Ruven Guna
  • 414
  • 7
  • 25
  • 2
    Would `pd.factorize(df['id'])[0] + 1` work for your use case? – Jon Clements Aug 24 '18 at 09:03
  • Possible duplicate of [Pandas: convert categories to numbers](https://stackoverflow.com/questions/38088652/pandas-convert-categories-to-numbers) – jpp Aug 24 '18 at 09:07
  • Possible duplicate of [Pandas: convert categories to numbers](https://stackoverflow.com/questions/38088652/pandas-convert-categories-to-numbers) – Sociopath Aug 24 '18 at 11:03

1 Answers1

0

You can use dense rank():

df['id'] = df['id'].rank(method='dense').astype(int)
zipa
  • 27,316
  • 6
  • 40
  • 58