Search code examples
rpython-3.xpandasjoinregexp-replace

How will I write an equivalent Python syntax for R syntax


data$GroupId = paste( data$Surname, data$Pclass, sub('.$','X',data$Ticket), data$Fare, data$Embarked, sep='-')

Output: Output of the following code


Solution

  • This should work

    data['GroupId'] = data['Surname'] + data['Pclass'].astype(str).str.replace('$', 'X') + '-' + data['Fare'].astype(str) + data['Embarked'].astype(str)
    

    I don't know types of your dataframe so I converted them all to strings.

    If you want to see more see this so question