def fuzhi(x):
i=0
for x in df3['str']:
x==i
i=i+1
print(i)
if i>=len(df3['str']):
break
df3.loc[:,"strlist"] = df3.apply(fuzhi,axis=1)
该怎么实现下边这样的效果,也就是生成strlist这一列,比如第一个数据a,他就是0,然后再次出现在str这一列,他还是0,以此类推;b是1,再次出现在str这一列,还是1
求解答
#!/bin/env python
import pandas as pd
from pandas.core.frame import DataFrame
def getValue(rawList):
num = 0
newDict = {}
strList = []
for i in rawList:
if i not in strList:
newDict[i] = num
strList.append(i)
num += 1
resultList = [newDict[b] for b in rawList]
newDataFrame = DataFrame({"str":rawList, "strlist":resultList})
print(newDataFrame)
#data = pd.read_table("test")
#将你的str列转换成列表
rawList = data['str'].tolist()
#getValue(['a', 'b', 'c', 'a', 'd', 'c', 'b', 'a', 'e'])
getValue(rawList)
365