简体   繁体   English

使用python更新数据库特定列中的值

[英]Updating values in a particular column of a database using python

I have a col called id in a dataframe called _newdata which looks like this. 我在名为_newdata的数据框中有一个名为id的col,它看起来像这样。 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. 我想做的是用值重命名“ id”,以便它以连续数字表示。 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

您可以使用致密 rank()

df['id'] = df['id'].rank(method='dense').astype(int)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM