Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

group split apply pandas python

I have a data set like this

Input Data

I want to roll up the data on Top Level GUID while I do that I would concatenate the DocID with the following condition.

  • if the DocID are in series then use hyphen separator and
  • if the DocIDs are not in series then use a colon separator.

Output Data

Create the following function which works fine however it doesn't work when using groupby.apply function.

alist=[1, 2, 6, 4,5]
inputstring=['abc','cyz','mdv','1','tds']

FinalOutput = []

def createstrings(alist,inputstring):
    Loopoutput = inputstring[0]
    for i in range(len(alist)-1):
        if alist[i]+1 == alist[i+1]:
            Loopoutput += " - "+str(inputstring[i+1])
        else:
            Loopoutput += ";"+str((inputstring[i+1]))
    FinalOutput.append(Loopoutput)
    return(FinalOutput)

createstrings(alist,inputstring)


df['Stripped_DocID'] = pd.to_numeric(df['DocID'].str.split('_').str[1])
df.groupby('Top Level GUID').apply(createstrings('Stripped_DocID','DocID'))

error I am getting is TypeError: can only concatenate str (not "int") to str


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...