Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 重命名数据帧中的相同标签_Python_Pandas_Dataframe - Fatal编程技术网

Python 重命名数据帧中的相同标签

Python 重命名数据帧中的相同标签,python,pandas,dataframe,Python,Pandas,Dataframe,我现在没有我的代码,我不能发布它;但是,我为数据帧执行.drop_duplicates方法,并且有多个具有相同标签的行。如何重命名标签,使名称Terry变为Terry 1、Terry 2、Terry 3等等 下面是一些模拟代码: series1 = ['M','M','S','S','S','T','T'] series2 = [1,2,1,1,3,4,4] series3 = [2,4,2,2,2,2,2] data = pd.DataFrame.from_dict({'Name':serie

我现在没有我的代码,我不能发布它;但是,我为数据帧执行.drop_duplicates方法,并且有多个具有相同标签的行。如何重命名标签,使名称Terry变为Terry 1、Terry 2、Terry 3等等

下面是一些模拟代码:

series1 = ['M','M','S','S','S','T','T']
series2 = [1,2,1,1,3,4,4]
series3 = [2,4,2,2,2,2,2]
data = pd.DataFrame.from_dict({'Name':series1,'Number 1': series2,'Number 2':series3})
我正在尝试将名称更改为M_0、M_1、S_0、S_1、S_0等。。。基于它是否与以前的标签匹配,这可能会有所帮助。 我的示例df:

       col2
col1       
Jack      1
Terry     2
Paul      3
Terry     4
Terry     5
然后,我将把索引放在一个列表中,并在解决方案中使用Jochen Ritzel的函数来处理重复项。我修改了他的函数,在重复的索引后添加一个数字

功能如下:

def rename_duplicates(old): 
        seen = {} 
        for x in old: 
            if x in seen: 
                seen[x] += 1 
                yield "%s%d" % (x, seen[x]) 
            else: 
                seen[x] = 0 
                yield x 
然后存储新索引:

new_index = list(rename_duplicates(indexList))
现在,您只需将当前df中的索引更改为新索引,如下所示:

df.reset_index(drop=True, inplace=True)
df.index = new_index

我现在没有可用的代码,我无法发布,请在您发布之前不要发布您的问题。很抱歉,我无法发布我的代码。