Convert specific parts of string to uppercase?










2














I have a DataFrame and I'd like to make only specific parts of strings to be made uppercase with an underscore afterwords.



| TYPE | NAME |
|-----------------------------|
| Contract Employee | John |
| Full Time Employee | Carol |
| Temporary Employee | Kyle |


I'd like the words "Contract" and "Temporary" made into uppercase like this with an underscore after and before the word:



| TYPE | NAME |
|-------------------------------|
| _CONTRACT_ Employee | John |
| Full Time Employee | Carol |
| _TEMPORARY_ Employee | Kyle |


I tried using str.upper() but that made the entire cell uppercase and I'm looking only for those certain words.



EDIT: I should mention sometimes the words are not capitalized if that matters. Often it will display as temporary employee instead of Temporary Employee.










share|improve this question























  • Using replace with dict is simplest one in this use case, which panda provides.
    – pygo
    Nov 12 at 6:10
















2














I have a DataFrame and I'd like to make only specific parts of strings to be made uppercase with an underscore afterwords.



| TYPE | NAME |
|-----------------------------|
| Contract Employee | John |
| Full Time Employee | Carol |
| Temporary Employee | Kyle |


I'd like the words "Contract" and "Temporary" made into uppercase like this with an underscore after and before the word:



| TYPE | NAME |
|-------------------------------|
| _CONTRACT_ Employee | John |
| Full Time Employee | Carol |
| _TEMPORARY_ Employee | Kyle |


I tried using str.upper() but that made the entire cell uppercase and I'm looking only for those certain words.



EDIT: I should mention sometimes the words are not capitalized if that matters. Often it will display as temporary employee instead of Temporary Employee.










share|improve this question























  • Using replace with dict is simplest one in this use case, which panda provides.
    – pygo
    Nov 12 at 6:10














2












2








2







I have a DataFrame and I'd like to make only specific parts of strings to be made uppercase with an underscore afterwords.



| TYPE | NAME |
|-----------------------------|
| Contract Employee | John |
| Full Time Employee | Carol |
| Temporary Employee | Kyle |


I'd like the words "Contract" and "Temporary" made into uppercase like this with an underscore after and before the word:



| TYPE | NAME |
|-------------------------------|
| _CONTRACT_ Employee | John |
| Full Time Employee | Carol |
| _TEMPORARY_ Employee | Kyle |


I tried using str.upper() but that made the entire cell uppercase and I'm looking only for those certain words.



EDIT: I should mention sometimes the words are not capitalized if that matters. Often it will display as temporary employee instead of Temporary Employee.










share|improve this question















I have a DataFrame and I'd like to make only specific parts of strings to be made uppercase with an underscore afterwords.



| TYPE | NAME |
|-----------------------------|
| Contract Employee | John |
| Full Time Employee | Carol |
| Temporary Employee | Kyle |


I'd like the words "Contract" and "Temporary" made into uppercase like this with an underscore after and before the word:



| TYPE | NAME |
|-------------------------------|
| _CONTRACT_ Employee | John |
| Full Time Employee | Carol |
| _TEMPORARY_ Employee | Kyle |


I tried using str.upper() but that made the entire cell uppercase and I'm looking only for those certain words.



EDIT: I should mention sometimes the words are not capitalized if that matters. Often it will display as temporary employee instead of Temporary Employee.







python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 5:25

























asked Nov 12 at 5:22









ATCH_torn

301111




301111











  • Using replace with dict is simplest one in this use case, which panda provides.
    – pygo
    Nov 12 at 6:10

















  • Using replace with dict is simplest one in this use case, which panda provides.
    – pygo
    Nov 12 at 6:10
















Using replace with dict is simplest one in this use case, which panda provides.
– pygo
Nov 12 at 6:10





Using replace with dict is simplest one in this use case, which panda provides.
– pygo
Nov 12 at 6:10













5 Answers
5






active

oldest

votes


















6














Here is one option using re.sub:



def type_to_upper(match):
return match.group(1).upper()

text = "Contract Employee"
output = re.sub(r'b(Contract|Temporary)b', type_to_upper, text)


EDIT:



This is the same approach applied within pandas, also addressing the latest edit regarding uncertain upper or lower case words to be replaced:



test dataframe:



 TYPE NAME
0 Contract Employee John
1 Full Time Employee Carol
2 Temporary Employee Kyle
3 contract employee John
4 Full Time employee Carol
5 temporary employee Kyle


solution:



def type_to_upper(match):
return '__'.format(match.group(1).upper())

df.TYPE = df.TYPE.str.replace(r'b([Cc]ontract|[Tt]emporary)b', type_to_upper)


result:



df 
TYPE NAME
0 _CONTRACT_ Employee John
1 Full Time Employee Carol
2 _TEMPORARY_ Employee Kyle
3 _CONTRACT_ employee John
4 Full Time employee Carol
5 _TEMPORARY_ employee Kyle


Note that this is only for addressing exactly these two cases which are defined in the OPs request. For complete case insensitivity it's even simpler:



df.TYPE = df.TYPE.str.replace(r'b(contract|temporary)b', type_to_upper, case=False)





share|improve this answer






















  • But implementing into pandas dataframe?
    – U9-Forward
    Nov 12 at 5:25






  • 1




    @U9-Forward I know zero about Pandas data frames. I will let the OP comment if this answer is really not helpful at all.
    – Tim Biegeleisen
    Nov 12 at 5:26










  • It's okay, still nice :-)
    – U9-Forward
    Nov 12 at 5:28










  • I do need it in a pandas dataframe. Theoretically I could apply it to the column but I'd like a more inline solution rather than creating unnecessary functions.
    – ATCH_torn
    Nov 12 at 5:29










  • @ATCH_torn Fair enough. I can delete this if a better answer comes along.
    – Tim Biegeleisen
    Nov 12 at 5:29


















2














Something that modifies the data-frame (without regex or anything):



l=['Contract','Temporary']
df['TYPE']=df['TYPE'].apply(lambda x: ' '.join(['_'+i.upper()+'_' if i in l else i for i in x.split()]))


join and split, being in a apply.



And then now:



print(df)


Is:



 TYPE NAME
0 _CONTRACT_ Employee John
1 Full Time Employee Carol
2 _TEMPORARY_ Employee Kyle





share|improve this answer




























    2














    This is simples and easy way by using replace with dictionary format.



    Please refer pandas Doc for Series.replace



    df["TYPE"] = df["TYPE"].replace('Contract': '_CONTRACT_', 'Temporary': '_Temporary_', regex=True)


    Just reproduced:



    >>> df
    TYPE Name
    0 Contract Employee John
    1 Full Time Employee Carol
    2 Temporary Employee Kyle

    >>> df["TYPE"] = df["TYPE"].replace('Contract': '_CONTRACT_', 'Temporary': '_TEMPORARY_', regex=True)
    >>> df
    TYPE Name
    0 _CONTRACT_ Employee John
    1 Full Time Employee Carol
    2 _TEMPORARY_ Employee Kyle





    share|improve this answer






















    • This doesn't work with the OP's EDIT, which tells that you can't rely on uppercase first characters.
      – SpghttCd
      Nov 12 at 6:13










    • @SpghttCd, this works , simply you need to place the new value in a way you want to look it, edited my answer..
      – pygo
      Nov 12 at 6:14











    • Sorry, but this in the meaning of this what you posted above does not. If you already took care about the EDIT, then IMO you should address it in your post, otherwise nobody can know.
      – SpghttCd
      Nov 12 at 6:21










    • @SpghttCd, sorry about that but its just a value name which can be modified as required as being simplest :- ) will take care for future reference
      – pygo
      Nov 12 at 6:23










    • Hmm - I think we're misunderstanding each other, as I don't understand what you mean by 'just a value name', but I feel that you think your code creates all the replacements required by the OP, which is not the case. Did you realize, that 'Contract' can often be 'contract' and the same with the upper 'T' of 'Temporary'?
      – SpghttCd
      Nov 12 at 6:28


















    1














    U9 beat me to it, using lambda and split() on the input:



    def match_and_upper(match):
    matches = ["Contract", "Temporary"]
    if match in matches:
    return match.upper()
    return match

    input = "Contract Employee"
    output = " ".join(map(lambda x: match_and_upper(x), input.split()))
    # Result: CONTRACT Employee #





    share|improve this answer




















    • You say i beat you to it :D , Happens to me times too :-)
      – U9-Forward
      Nov 12 at 5:48


















    1














    Answering part of my own question here. Using @Tim Biegeleisen's regex he provided, I did a string replace on the column.



    df["TYPE"] = df["TYPE"].str.replace(r'b(Contract)b', '_CONTRACT_')





    share|improve this answer




















    • @ ATCH_torn, see my answer thats is easy and no need to create a function or use lambda exp though.
      – pygo
      Nov 12 at 6:13










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256333%2fconvert-specific-parts-of-string-to-uppercase%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    Here is one option using re.sub:



    def type_to_upper(match):
    return match.group(1).upper()

    text = "Contract Employee"
    output = re.sub(r'b(Contract|Temporary)b', type_to_upper, text)


    EDIT:



    This is the same approach applied within pandas, also addressing the latest edit regarding uncertain upper or lower case words to be replaced:



    test dataframe:



     TYPE NAME
    0 Contract Employee John
    1 Full Time Employee Carol
    2 Temporary Employee Kyle
    3 contract employee John
    4 Full Time employee Carol
    5 temporary employee Kyle


    solution:



    def type_to_upper(match):
    return '__'.format(match.group(1).upper())

    df.TYPE = df.TYPE.str.replace(r'b([Cc]ontract|[Tt]emporary)b', type_to_upper)


    result:



    df 
    TYPE NAME
    0 _CONTRACT_ Employee John
    1 Full Time Employee Carol
    2 _TEMPORARY_ Employee Kyle
    3 _CONTRACT_ employee John
    4 Full Time employee Carol
    5 _TEMPORARY_ employee Kyle


    Note that this is only for addressing exactly these two cases which are defined in the OPs request. For complete case insensitivity it's even simpler:



    df.TYPE = df.TYPE.str.replace(r'b(contract|temporary)b', type_to_upper, case=False)





    share|improve this answer






















    • But implementing into pandas dataframe?
      – U9-Forward
      Nov 12 at 5:25






    • 1




      @U9-Forward I know zero about Pandas data frames. I will let the OP comment if this answer is really not helpful at all.
      – Tim Biegeleisen
      Nov 12 at 5:26










    • It's okay, still nice :-)
      – U9-Forward
      Nov 12 at 5:28










    • I do need it in a pandas dataframe. Theoretically I could apply it to the column but I'd like a more inline solution rather than creating unnecessary functions.
      – ATCH_torn
      Nov 12 at 5:29










    • @ATCH_torn Fair enough. I can delete this if a better answer comes along.
      – Tim Biegeleisen
      Nov 12 at 5:29















    6














    Here is one option using re.sub:



    def type_to_upper(match):
    return match.group(1).upper()

    text = "Contract Employee"
    output = re.sub(r'b(Contract|Temporary)b', type_to_upper, text)


    EDIT:



    This is the same approach applied within pandas, also addressing the latest edit regarding uncertain upper or lower case words to be replaced:



    test dataframe:



     TYPE NAME
    0 Contract Employee John
    1 Full Time Employee Carol
    2 Temporary Employee Kyle
    3 contract employee John
    4 Full Time employee Carol
    5 temporary employee Kyle


    solution:



    def type_to_upper(match):
    return '__'.format(match.group(1).upper())

    df.TYPE = df.TYPE.str.replace(r'b([Cc]ontract|[Tt]emporary)b', type_to_upper)


    result:



    df 
    TYPE NAME
    0 _CONTRACT_ Employee John
    1 Full Time Employee Carol
    2 _TEMPORARY_ Employee Kyle
    3 _CONTRACT_ employee John
    4 Full Time employee Carol
    5 _TEMPORARY_ employee Kyle


    Note that this is only for addressing exactly these two cases which are defined in the OPs request. For complete case insensitivity it's even simpler:



    df.TYPE = df.TYPE.str.replace(r'b(contract|temporary)b', type_to_upper, case=False)





    share|improve this answer






















    • But implementing into pandas dataframe?
      – U9-Forward
      Nov 12 at 5:25






    • 1




      @U9-Forward I know zero about Pandas data frames. I will let the OP comment if this answer is really not helpful at all.
      – Tim Biegeleisen
      Nov 12 at 5:26










    • It's okay, still nice :-)
      – U9-Forward
      Nov 12 at 5:28










    • I do need it in a pandas dataframe. Theoretically I could apply it to the column but I'd like a more inline solution rather than creating unnecessary functions.
      – ATCH_torn
      Nov 12 at 5:29










    • @ATCH_torn Fair enough. I can delete this if a better answer comes along.
      – Tim Biegeleisen
      Nov 12 at 5:29













    6












    6








    6






    Here is one option using re.sub:



    def type_to_upper(match):
    return match.group(1).upper()

    text = "Contract Employee"
    output = re.sub(r'b(Contract|Temporary)b', type_to_upper, text)


    EDIT:



    This is the same approach applied within pandas, also addressing the latest edit regarding uncertain upper or lower case words to be replaced:



    test dataframe:



     TYPE NAME
    0 Contract Employee John
    1 Full Time Employee Carol
    2 Temporary Employee Kyle
    3 contract employee John
    4 Full Time employee Carol
    5 temporary employee Kyle


    solution:



    def type_to_upper(match):
    return '__'.format(match.group(1).upper())

    df.TYPE = df.TYPE.str.replace(r'b([Cc]ontract|[Tt]emporary)b', type_to_upper)


    result:



    df 
    TYPE NAME
    0 _CONTRACT_ Employee John
    1 Full Time Employee Carol
    2 _TEMPORARY_ Employee Kyle
    3 _CONTRACT_ employee John
    4 Full Time employee Carol
    5 _TEMPORARY_ employee Kyle


    Note that this is only for addressing exactly these two cases which are defined in the OPs request. For complete case insensitivity it's even simpler:



    df.TYPE = df.TYPE.str.replace(r'b(contract|temporary)b', type_to_upper, case=False)





    share|improve this answer














    Here is one option using re.sub:



    def type_to_upper(match):
    return match.group(1).upper()

    text = "Contract Employee"
    output = re.sub(r'b(Contract|Temporary)b', type_to_upper, text)


    EDIT:



    This is the same approach applied within pandas, also addressing the latest edit regarding uncertain upper or lower case words to be replaced:



    test dataframe:



     TYPE NAME
    0 Contract Employee John
    1 Full Time Employee Carol
    2 Temporary Employee Kyle
    3 contract employee John
    4 Full Time employee Carol
    5 temporary employee Kyle


    solution:



    def type_to_upper(match):
    return '__'.format(match.group(1).upper())

    df.TYPE = df.TYPE.str.replace(r'b([Cc]ontract|[Tt]emporary)b', type_to_upper)


    result:



    df 
    TYPE NAME
    0 _CONTRACT_ Employee John
    1 Full Time Employee Carol
    2 _TEMPORARY_ Employee Kyle
    3 _CONTRACT_ employee John
    4 Full Time employee Carol
    5 _TEMPORARY_ employee Kyle


    Note that this is only for addressing exactly these two cases which are defined in the OPs request. For complete case insensitivity it's even simpler:



    df.TYPE = df.TYPE.str.replace(r'b(contract|temporary)b', type_to_upper, case=False)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 at 8:58









    SpghttCd

    3,9122313




    3,9122313










    answered Nov 12 at 5:25









    Tim Biegeleisen

    215k1386137




    215k1386137











    • But implementing into pandas dataframe?
      – U9-Forward
      Nov 12 at 5:25






    • 1




      @U9-Forward I know zero about Pandas data frames. I will let the OP comment if this answer is really not helpful at all.
      – Tim Biegeleisen
      Nov 12 at 5:26










    • It's okay, still nice :-)
      – U9-Forward
      Nov 12 at 5:28










    • I do need it in a pandas dataframe. Theoretically I could apply it to the column but I'd like a more inline solution rather than creating unnecessary functions.
      – ATCH_torn
      Nov 12 at 5:29










    • @ATCH_torn Fair enough. I can delete this if a better answer comes along.
      – Tim Biegeleisen
      Nov 12 at 5:29
















    • But implementing into pandas dataframe?
      – U9-Forward
      Nov 12 at 5:25






    • 1




      @U9-Forward I know zero about Pandas data frames. I will let the OP comment if this answer is really not helpful at all.
      – Tim Biegeleisen
      Nov 12 at 5:26










    • It's okay, still nice :-)
      – U9-Forward
      Nov 12 at 5:28










    • I do need it in a pandas dataframe. Theoretically I could apply it to the column but I'd like a more inline solution rather than creating unnecessary functions.
      – ATCH_torn
      Nov 12 at 5:29










    • @ATCH_torn Fair enough. I can delete this if a better answer comes along.
      – Tim Biegeleisen
      Nov 12 at 5:29















    But implementing into pandas dataframe?
    – U9-Forward
    Nov 12 at 5:25




    But implementing into pandas dataframe?
    – U9-Forward
    Nov 12 at 5:25




    1




    1




    @U9-Forward I know zero about Pandas data frames. I will let the OP comment if this answer is really not helpful at all.
    – Tim Biegeleisen
    Nov 12 at 5:26




    @U9-Forward I know zero about Pandas data frames. I will let the OP comment if this answer is really not helpful at all.
    – Tim Biegeleisen
    Nov 12 at 5:26












    It's okay, still nice :-)
    – U9-Forward
    Nov 12 at 5:28




    It's okay, still nice :-)
    – U9-Forward
    Nov 12 at 5:28












    I do need it in a pandas dataframe. Theoretically I could apply it to the column but I'd like a more inline solution rather than creating unnecessary functions.
    – ATCH_torn
    Nov 12 at 5:29




    I do need it in a pandas dataframe. Theoretically I could apply it to the column but I'd like a more inline solution rather than creating unnecessary functions.
    – ATCH_torn
    Nov 12 at 5:29












    @ATCH_torn Fair enough. I can delete this if a better answer comes along.
    – Tim Biegeleisen
    Nov 12 at 5:29




    @ATCH_torn Fair enough. I can delete this if a better answer comes along.
    – Tim Biegeleisen
    Nov 12 at 5:29













    2














    Something that modifies the data-frame (without regex or anything):



    l=['Contract','Temporary']
    df['TYPE']=df['TYPE'].apply(lambda x: ' '.join(['_'+i.upper()+'_' if i in l else i for i in x.split()]))


    join and split, being in a apply.



    And then now:



    print(df)


    Is:



     TYPE NAME
    0 _CONTRACT_ Employee John
    1 Full Time Employee Carol
    2 _TEMPORARY_ Employee Kyle





    share|improve this answer

























      2














      Something that modifies the data-frame (without regex or anything):



      l=['Contract','Temporary']
      df['TYPE']=df['TYPE'].apply(lambda x: ' '.join(['_'+i.upper()+'_' if i in l else i for i in x.split()]))


      join and split, being in a apply.



      And then now:



      print(df)


      Is:



       TYPE NAME
      0 _CONTRACT_ Employee John
      1 Full Time Employee Carol
      2 _TEMPORARY_ Employee Kyle





      share|improve this answer























        2












        2








        2






        Something that modifies the data-frame (without regex or anything):



        l=['Contract','Temporary']
        df['TYPE']=df['TYPE'].apply(lambda x: ' '.join(['_'+i.upper()+'_' if i in l else i for i in x.split()]))


        join and split, being in a apply.



        And then now:



        print(df)


        Is:



         TYPE NAME
        0 _CONTRACT_ Employee John
        1 Full Time Employee Carol
        2 _TEMPORARY_ Employee Kyle





        share|improve this answer












        Something that modifies the data-frame (without regex or anything):



        l=['Contract','Temporary']
        df['TYPE']=df['TYPE'].apply(lambda x: ' '.join(['_'+i.upper()+'_' if i in l else i for i in x.split()]))


        join and split, being in a apply.



        And then now:



        print(df)


        Is:



         TYPE NAME
        0 _CONTRACT_ Employee John
        1 Full Time Employee Carol
        2 _TEMPORARY_ Employee Kyle






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 5:35









        U9-Forward

        11.8k21136




        11.8k21136





















            2














            This is simples and easy way by using replace with dictionary format.



            Please refer pandas Doc for Series.replace



            df["TYPE"] = df["TYPE"].replace('Contract': '_CONTRACT_', 'Temporary': '_Temporary_', regex=True)


            Just reproduced:



            >>> df
            TYPE Name
            0 Contract Employee John
            1 Full Time Employee Carol
            2 Temporary Employee Kyle

            >>> df["TYPE"] = df["TYPE"].replace('Contract': '_CONTRACT_', 'Temporary': '_TEMPORARY_', regex=True)
            >>> df
            TYPE Name
            0 _CONTRACT_ Employee John
            1 Full Time Employee Carol
            2 _TEMPORARY_ Employee Kyle





            share|improve this answer






















            • This doesn't work with the OP's EDIT, which tells that you can't rely on uppercase first characters.
              – SpghttCd
              Nov 12 at 6:13










            • @SpghttCd, this works , simply you need to place the new value in a way you want to look it, edited my answer..
              – pygo
              Nov 12 at 6:14











            • Sorry, but this in the meaning of this what you posted above does not. If you already took care about the EDIT, then IMO you should address it in your post, otherwise nobody can know.
              – SpghttCd
              Nov 12 at 6:21










            • @SpghttCd, sorry about that but its just a value name which can be modified as required as being simplest :- ) will take care for future reference
              – pygo
              Nov 12 at 6:23










            • Hmm - I think we're misunderstanding each other, as I don't understand what you mean by 'just a value name', but I feel that you think your code creates all the replacements required by the OP, which is not the case. Did you realize, that 'Contract' can often be 'contract' and the same with the upper 'T' of 'Temporary'?
              – SpghttCd
              Nov 12 at 6:28















            2














            This is simples and easy way by using replace with dictionary format.



            Please refer pandas Doc for Series.replace



            df["TYPE"] = df["TYPE"].replace('Contract': '_CONTRACT_', 'Temporary': '_Temporary_', regex=True)


            Just reproduced:



            >>> df
            TYPE Name
            0 Contract Employee John
            1 Full Time Employee Carol
            2 Temporary Employee Kyle

            >>> df["TYPE"] = df["TYPE"].replace('Contract': '_CONTRACT_', 'Temporary': '_TEMPORARY_', regex=True)
            >>> df
            TYPE Name
            0 _CONTRACT_ Employee John
            1 Full Time Employee Carol
            2 _TEMPORARY_ Employee Kyle





            share|improve this answer






















            • This doesn't work with the OP's EDIT, which tells that you can't rely on uppercase first characters.
              – SpghttCd
              Nov 12 at 6:13










            • @SpghttCd, this works , simply you need to place the new value in a way you want to look it, edited my answer..
              – pygo
              Nov 12 at 6:14











            • Sorry, but this in the meaning of this what you posted above does not. If you already took care about the EDIT, then IMO you should address it in your post, otherwise nobody can know.
              – SpghttCd
              Nov 12 at 6:21










            • @SpghttCd, sorry about that but its just a value name which can be modified as required as being simplest :- ) will take care for future reference
              – pygo
              Nov 12 at 6:23










            • Hmm - I think we're misunderstanding each other, as I don't understand what you mean by 'just a value name', but I feel that you think your code creates all the replacements required by the OP, which is not the case. Did you realize, that 'Contract' can often be 'contract' and the same with the upper 'T' of 'Temporary'?
              – SpghttCd
              Nov 12 at 6:28













            2












            2








            2






            This is simples and easy way by using replace with dictionary format.



            Please refer pandas Doc for Series.replace



            df["TYPE"] = df["TYPE"].replace('Contract': '_CONTRACT_', 'Temporary': '_Temporary_', regex=True)


            Just reproduced:



            >>> df
            TYPE Name
            0 Contract Employee John
            1 Full Time Employee Carol
            2 Temporary Employee Kyle

            >>> df["TYPE"] = df["TYPE"].replace('Contract': '_CONTRACT_', 'Temporary': '_TEMPORARY_', regex=True)
            >>> df
            TYPE Name
            0 _CONTRACT_ Employee John
            1 Full Time Employee Carol
            2 _TEMPORARY_ Employee Kyle





            share|improve this answer














            This is simples and easy way by using replace with dictionary format.



            Please refer pandas Doc for Series.replace



            df["TYPE"] = df["TYPE"].replace('Contract': '_CONTRACT_', 'Temporary': '_Temporary_', regex=True)


            Just reproduced:



            >>> df
            TYPE Name
            0 Contract Employee John
            1 Full Time Employee Carol
            2 Temporary Employee Kyle

            >>> df["TYPE"] = df["TYPE"].replace('Contract': '_CONTRACT_', 'Temporary': '_TEMPORARY_', regex=True)
            >>> df
            TYPE Name
            0 _CONTRACT_ Employee John
            1 Full Time Employee Carol
            2 _TEMPORARY_ Employee Kyle






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 12 at 6:40

























            answered Nov 12 at 5:57









            pygo

            1,9171616




            1,9171616











            • This doesn't work with the OP's EDIT, which tells that you can't rely on uppercase first characters.
              – SpghttCd
              Nov 12 at 6:13










            • @SpghttCd, this works , simply you need to place the new value in a way you want to look it, edited my answer..
              – pygo
              Nov 12 at 6:14











            • Sorry, but this in the meaning of this what you posted above does not. If you already took care about the EDIT, then IMO you should address it in your post, otherwise nobody can know.
              – SpghttCd
              Nov 12 at 6:21










            • @SpghttCd, sorry about that but its just a value name which can be modified as required as being simplest :- ) will take care for future reference
              – pygo
              Nov 12 at 6:23










            • Hmm - I think we're misunderstanding each other, as I don't understand what you mean by 'just a value name', but I feel that you think your code creates all the replacements required by the OP, which is not the case. Did you realize, that 'Contract' can often be 'contract' and the same with the upper 'T' of 'Temporary'?
              – SpghttCd
              Nov 12 at 6:28
















            • This doesn't work with the OP's EDIT, which tells that you can't rely on uppercase first characters.
              – SpghttCd
              Nov 12 at 6:13










            • @SpghttCd, this works , simply you need to place the new value in a way you want to look it, edited my answer..
              – pygo
              Nov 12 at 6:14











            • Sorry, but this in the meaning of this what you posted above does not. If you already took care about the EDIT, then IMO you should address it in your post, otherwise nobody can know.
              – SpghttCd
              Nov 12 at 6:21










            • @SpghttCd, sorry about that but its just a value name which can be modified as required as being simplest :- ) will take care for future reference
              – pygo
              Nov 12 at 6:23










            • Hmm - I think we're misunderstanding each other, as I don't understand what you mean by 'just a value name', but I feel that you think your code creates all the replacements required by the OP, which is not the case. Did you realize, that 'Contract' can often be 'contract' and the same with the upper 'T' of 'Temporary'?
              – SpghttCd
              Nov 12 at 6:28















            This doesn't work with the OP's EDIT, which tells that you can't rely on uppercase first characters.
            – SpghttCd
            Nov 12 at 6:13




            This doesn't work with the OP's EDIT, which tells that you can't rely on uppercase first characters.
            – SpghttCd
            Nov 12 at 6:13












            @SpghttCd, this works , simply you need to place the new value in a way you want to look it, edited my answer..
            – pygo
            Nov 12 at 6:14





            @SpghttCd, this works , simply you need to place the new value in a way you want to look it, edited my answer..
            – pygo
            Nov 12 at 6:14













            Sorry, but this in the meaning of this what you posted above does not. If you already took care about the EDIT, then IMO you should address it in your post, otherwise nobody can know.
            – SpghttCd
            Nov 12 at 6:21




            Sorry, but this in the meaning of this what you posted above does not. If you already took care about the EDIT, then IMO you should address it in your post, otherwise nobody can know.
            – SpghttCd
            Nov 12 at 6:21












            @SpghttCd, sorry about that but its just a value name which can be modified as required as being simplest :- ) will take care for future reference
            – pygo
            Nov 12 at 6:23




            @SpghttCd, sorry about that but its just a value name which can be modified as required as being simplest :- ) will take care for future reference
            – pygo
            Nov 12 at 6:23












            Hmm - I think we're misunderstanding each other, as I don't understand what you mean by 'just a value name', but I feel that you think your code creates all the replacements required by the OP, which is not the case. Did you realize, that 'Contract' can often be 'contract' and the same with the upper 'T' of 'Temporary'?
            – SpghttCd
            Nov 12 at 6:28




            Hmm - I think we're misunderstanding each other, as I don't understand what you mean by 'just a value name', but I feel that you think your code creates all the replacements required by the OP, which is not the case. Did you realize, that 'Contract' can often be 'contract' and the same with the upper 'T' of 'Temporary'?
            – SpghttCd
            Nov 12 at 6:28











            1














            U9 beat me to it, using lambda and split() on the input:



            def match_and_upper(match):
            matches = ["Contract", "Temporary"]
            if match in matches:
            return match.upper()
            return match

            input = "Contract Employee"
            output = " ".join(map(lambda x: match_and_upper(x), input.split()))
            # Result: CONTRACT Employee #





            share|improve this answer




















            • You say i beat you to it :D , Happens to me times too :-)
              – U9-Forward
              Nov 12 at 5:48















            1














            U9 beat me to it, using lambda and split() on the input:



            def match_and_upper(match):
            matches = ["Contract", "Temporary"]
            if match in matches:
            return match.upper()
            return match

            input = "Contract Employee"
            output = " ".join(map(lambda x: match_and_upper(x), input.split()))
            # Result: CONTRACT Employee #





            share|improve this answer




















            • You say i beat you to it :D , Happens to me times too :-)
              – U9-Forward
              Nov 12 at 5:48













            1












            1








            1






            U9 beat me to it, using lambda and split() on the input:



            def match_and_upper(match):
            matches = ["Contract", "Temporary"]
            if match in matches:
            return match.upper()
            return match

            input = "Contract Employee"
            output = " ".join(map(lambda x: match_and_upper(x), input.split()))
            # Result: CONTRACT Employee #





            share|improve this answer












            U9 beat me to it, using lambda and split() on the input:



            def match_and_upper(match):
            matches = ["Contract", "Temporary"]
            if match in matches:
            return match.upper()
            return match

            input = "Contract Employee"
            output = " ".join(map(lambda x: match_and_upper(x), input.split()))
            # Result: CONTRACT Employee #






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 at 5:42









            Green Cell

            1,836825




            1,836825











            • You say i beat you to it :D , Happens to me times too :-)
              – U9-Forward
              Nov 12 at 5:48
















            • You say i beat you to it :D , Happens to me times too :-)
              – U9-Forward
              Nov 12 at 5:48















            You say i beat you to it :D , Happens to me times too :-)
            – U9-Forward
            Nov 12 at 5:48




            You say i beat you to it :D , Happens to me times too :-)
            – U9-Forward
            Nov 12 at 5:48











            1














            Answering part of my own question here. Using @Tim Biegeleisen's regex he provided, I did a string replace on the column.



            df["TYPE"] = df["TYPE"].str.replace(r'b(Contract)b', '_CONTRACT_')





            share|improve this answer




















            • @ ATCH_torn, see my answer thats is easy and no need to create a function or use lambda exp though.
              – pygo
              Nov 12 at 6:13















            1














            Answering part of my own question here. Using @Tim Biegeleisen's regex he provided, I did a string replace on the column.



            df["TYPE"] = df["TYPE"].str.replace(r'b(Contract)b', '_CONTRACT_')





            share|improve this answer




















            • @ ATCH_torn, see my answer thats is easy and no need to create a function or use lambda exp though.
              – pygo
              Nov 12 at 6:13













            1












            1








            1






            Answering part of my own question here. Using @Tim Biegeleisen's regex he provided, I did a string replace on the column.



            df["TYPE"] = df["TYPE"].str.replace(r'b(Contract)b', '_CONTRACT_')





            share|improve this answer












            Answering part of my own question here. Using @Tim Biegeleisen's regex he provided, I did a string replace on the column.



            df["TYPE"] = df["TYPE"].str.replace(r'b(Contract)b', '_CONTRACT_')






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 at 5:42









            ATCH_torn

            301111




            301111











            • @ ATCH_torn, see my answer thats is easy and no need to create a function or use lambda exp though.
              – pygo
              Nov 12 at 6:13
















            • @ ATCH_torn, see my answer thats is easy and no need to create a function or use lambda exp though.
              – pygo
              Nov 12 at 6:13















            @ ATCH_torn, see my answer thats is easy and no need to create a function or use lambda exp though.
            – pygo
            Nov 12 at 6:13




            @ ATCH_torn, see my answer thats is easy and no need to create a function or use lambda exp though.
            – pygo
            Nov 12 at 6:13

















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256333%2fconvert-specific-parts-of-string-to-uppercase%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3