Searching for an item within a list in a column and saving that item to a new columnFinding the index of an item given a list containing it in PythonHow to randomly select an item from a list?How to make a new List in JavaHow to remove items from a list while iterating?How can I count the occurrences of a list item?How to remove the first Item from a list?Python list of dictionaries searchConcatenate item in list to stringsAdding new column to existing DataFrame in Python pandasGet list from pandas DataFrame column headers

Thread-safe, Convenient and Performant Random Number Generator

What happens when I copy a legendary creature with Rite of Replication?

What professions would a medieval village with a population of 100 need?

How to compare two different formulations of a problem?

What's /System/Volumes/Data?

What are the pros and cons of Einstein-Cartan Theory?

Turn TDE off when restoring SQL databases

Overwrite file only if data

Why is Boris Johnson visiting only Paris & Berlin if every member of the EU needs to agree on a withdrawal deal?

How can I support the recycling, but not the new production of aluminum?

Why are delta bots so finicky?

Why we don't have vaccination against all diseases which are caused by microbes?

Are there nouns that change meaning based on gender?

Dark side of an exoplanet - if it was earth-like would its surface light be detectable?

Metal that glows when near pieces of itself

Can you be convicted for being a murderer twice?

How to persuade recruiters to send me the Job Description?

Do I have to learn /o/ or /ɔ/ separately?

Potential new partner angry about first collaboration - how to answer email to close up this encounter in a graceful manner

Was Tuvok bluffing when he said that Voyager's transporters rendered the Kazon weapons useless?

Why my earth simulation is slower than the reality?

Why don't we use Cavea-B

Why didn’t Doctor Strange stay in the original winning timeline?

Have only girls been born for a long time in this village?



Searching for an item within a list in a column and saving that item to a new column


Finding the index of an item given a list containing it in PythonHow to randomly select an item from a list?How to make a new List in JavaHow to remove items from a list while iterating?How can I count the occurrences of a list item?How to remove the first Item from a list?Python list of dictionaries searchConcatenate item in list to stringsAdding new column to existing DataFrame in Python pandasGet list from pandas DataFrame column headers






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















I am very new to Python and need help!



I want to search a column of a data frame for an item in a list and if found, store that item in a new column. My the location column is messy and am trying to extract a state abbreviation if there is one.



So far I have been able to find the columns where the search terms are found (I’m not sure if this is 100% correct), how would I take the search term that was found and store it in a new column?



state_search=('CO', 'CA', 'WI', 'VA', 'NY', 'PA', 'MA', 'TX',)

pattern = '|'.join(state_search)

state_jobs_df=jobs_data_df.loc[jobs_data_df['location'].str.contains(pattern), :]


I want to take the state that was found and store that in a new 'state' column. Thanks for any help.



enter image description here




print (jobs_data_df)
location
0 Madison, WI 53702
1 Senior Training Leader located in Raynham, MA ...
2 Dixon CA
3 Camphill, PA Weekends and nights
4 Charlottesville, VA Some travel required
5 Houston, TX
6 Denver, CO 80215
7 Respiratory Therapy Primary Location : TX- Som...









share|improve this question


























  • Since you're doing a regex search, I'd suppose to add b around state names, so that some SKY Hotel, NH won't match Kentucky.

    – 9000
    Mar 27 at 15:26











  • @9000 - yes, word boundaries - in my answer.

    – jezrael
    Mar 27 at 15:26











  • If my answer was helpful, don't forget accept it - click on the check mark beside the answer to toggle it from greyed out to filled in. Thanks.

    – jezrael
    Mar 27 at 15:45

















2















I am very new to Python and need help!



I want to search a column of a data frame for an item in a list and if found, store that item in a new column. My the location column is messy and am trying to extract a state abbreviation if there is one.



So far I have been able to find the columns where the search terms are found (I’m not sure if this is 100% correct), how would I take the search term that was found and store it in a new column?



state_search=('CO', 'CA', 'WI', 'VA', 'NY', 'PA', 'MA', 'TX',)

pattern = '|'.join(state_search)

state_jobs_df=jobs_data_df.loc[jobs_data_df['location'].str.contains(pattern), :]


I want to take the state that was found and store that in a new 'state' column. Thanks for any help.



enter image description here




print (jobs_data_df)
location
0 Madison, WI 53702
1 Senior Training Leader located in Raynham, MA ...
2 Dixon CA
3 Camphill, PA Weekends and nights
4 Charlottesville, VA Some travel required
5 Houston, TX
6 Denver, CO 80215
7 Respiratory Therapy Primary Location : TX- Som...









share|improve this question


























  • Since you're doing a regex search, I'd suppose to add b around state names, so that some SKY Hotel, NH won't match Kentucky.

    – 9000
    Mar 27 at 15:26











  • @9000 - yes, word boundaries - in my answer.

    – jezrael
    Mar 27 at 15:26











  • If my answer was helpful, don't forget accept it - click on the check mark beside the answer to toggle it from greyed out to filled in. Thanks.

    – jezrael
    Mar 27 at 15:45













2












2








2








I am very new to Python and need help!



I want to search a column of a data frame for an item in a list and if found, store that item in a new column. My the location column is messy and am trying to extract a state abbreviation if there is one.



So far I have been able to find the columns where the search terms are found (I’m not sure if this is 100% correct), how would I take the search term that was found and store it in a new column?



state_search=('CO', 'CA', 'WI', 'VA', 'NY', 'PA', 'MA', 'TX',)

pattern = '|'.join(state_search)

state_jobs_df=jobs_data_df.loc[jobs_data_df['location'].str.contains(pattern), :]


I want to take the state that was found and store that in a new 'state' column. Thanks for any help.



enter image description here




print (jobs_data_df)
location
0 Madison, WI 53702
1 Senior Training Leader located in Raynham, MA ...
2 Dixon CA
3 Camphill, PA Weekends and nights
4 Charlottesville, VA Some travel required
5 Houston, TX
6 Denver, CO 80215
7 Respiratory Therapy Primary Location : TX- Som...









share|improve this question
















I am very new to Python and need help!



I want to search a column of a data frame for an item in a list and if found, store that item in a new column. My the location column is messy and am trying to extract a state abbreviation if there is one.



So far I have been able to find the columns where the search terms are found (I’m not sure if this is 100% correct), how would I take the search term that was found and store it in a new column?



state_search=('CO', 'CA', 'WI', 'VA', 'NY', 'PA', 'MA', 'TX',)

pattern = '|'.join(state_search)

state_jobs_df=jobs_data_df.loc[jobs_data_df['location'].str.contains(pattern), :]


I want to take the state that was found and store that in a new 'state' column. Thanks for any help.



enter image description here




print (jobs_data_df)
location
0 Madison, WI 53702
1 Senior Training Leader located in Raynham, MA ...
2 Dixon CA
3 Camphill, PA Weekends and nights
4 Charlottesville, VA Some travel required
5 Houston, TX
6 Denver, CO 80215
7 Respiratory Therapy Primary Location : TX- Som...






python string pandas list search






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 15:31









jezrael

401k29 gold badges412 silver badges479 bronze badges




401k29 gold badges412 silver badges479 bronze badges










asked Mar 27 at 15:05









KatipidersKatipiders

132 bronze badges




132 bronze badges















  • Since you're doing a regex search, I'd suppose to add b around state names, so that some SKY Hotel, NH won't match Kentucky.

    – 9000
    Mar 27 at 15:26











  • @9000 - yes, word boundaries - in my answer.

    – jezrael
    Mar 27 at 15:26











  • If my answer was helpful, don't forget accept it - click on the check mark beside the answer to toggle it from greyed out to filled in. Thanks.

    – jezrael
    Mar 27 at 15:45

















  • Since you're doing a regex search, I'd suppose to add b around state names, so that some SKY Hotel, NH won't match Kentucky.

    – 9000
    Mar 27 at 15:26











  • @9000 - yes, word boundaries - in my answer.

    – jezrael
    Mar 27 at 15:26











  • If my answer was helpful, don't forget accept it - click on the check mark beside the answer to toggle it from greyed out to filled in. Thanks.

    – jezrael
    Mar 27 at 15:45
















Since you're doing a regex search, I'd suppose to add b around state names, so that some SKY Hotel, NH won't match Kentucky.

– 9000
Mar 27 at 15:26





Since you're doing a regex search, I'd suppose to add b around state names, so that some SKY Hotel, NH won't match Kentucky.

– 9000
Mar 27 at 15:26













@9000 - yes, word boundaries - in my answer.

– jezrael
Mar 27 at 15:26





@9000 - yes, word boundaries - in my answer.

– jezrael
Mar 27 at 15:26













If my answer was helpful, don't forget accept it - click on the check mark beside the answer to toggle it from greyed out to filled in. Thanks.

– jezrael
Mar 27 at 15:45





If my answer was helpful, don't forget accept it - click on the check mark beside the answer to toggle it from greyed out to filled in. Thanks.

– jezrael
Mar 27 at 15:45












2 Answers
2






active

oldest

votes


















1














Use Series.str.extract with word boundaries and filter non missing rows by Series.notna or DataFrame.dropna:



pat = '|'.join(r"bb".format(x) for x in state_search)
jobs_data_df['state'] = jobs_data_df['location'].str.extract('('+ pat + ')', expand=False)
jobs_data_df = jobs_data_df[jobs_data_df['state'].notna()]


Or:



jobs_data_df = jobs_data_df.dropna(subset=['state'])





share|improve this answer
































    0














    It's a bit hack-y, but a simpler solution might take a form similar to:



    for row in dataRows:
    for state in state_search:
    if state in row:
    #put state in correct column here
    break #should break just the inner loop; if that doesn't happen, delete this line


    It's probably helpful to think about how the underlying program would have to approach the problem (checking each row for a string that matches one of your states, then doing something with it), and go at that directly. Unless you're dealing with a huge load of data, it may not be worth going crazy fancy with regular expressions or the like.






    share|improve this answer





























      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%2f55380454%2fsearching-for-an-item-within-a-list-in-a-column-and-saving-that-item-to-a-new-co%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Use Series.str.extract with word boundaries and filter non missing rows by Series.notna or DataFrame.dropna:



      pat = '|'.join(r"bb".format(x) for x in state_search)
      jobs_data_df['state'] = jobs_data_df['location'].str.extract('('+ pat + ')', expand=False)
      jobs_data_df = jobs_data_df[jobs_data_df['state'].notna()]


      Or:



      jobs_data_df = jobs_data_df.dropna(subset=['state'])





      share|improve this answer





























        1














        Use Series.str.extract with word boundaries and filter non missing rows by Series.notna or DataFrame.dropna:



        pat = '|'.join(r"bb".format(x) for x in state_search)
        jobs_data_df['state'] = jobs_data_df['location'].str.extract('('+ pat + ')', expand=False)
        jobs_data_df = jobs_data_df[jobs_data_df['state'].notna()]


        Or:



        jobs_data_df = jobs_data_df.dropna(subset=['state'])





        share|improve this answer



























          1












          1








          1







          Use Series.str.extract with word boundaries and filter non missing rows by Series.notna or DataFrame.dropna:



          pat = '|'.join(r"bb".format(x) for x in state_search)
          jobs_data_df['state'] = jobs_data_df['location'].str.extract('('+ pat + ')', expand=False)
          jobs_data_df = jobs_data_df[jobs_data_df['state'].notna()]


          Or:



          jobs_data_df = jobs_data_df.dropna(subset=['state'])





          share|improve this answer













          Use Series.str.extract with word boundaries and filter non missing rows by Series.notna or DataFrame.dropna:



          pat = '|'.join(r"bb".format(x) for x in state_search)
          jobs_data_df['state'] = jobs_data_df['location'].str.extract('('+ pat + ')', expand=False)
          jobs_data_df = jobs_data_df[jobs_data_df['state'].notna()]


          Or:



          jobs_data_df = jobs_data_df.dropna(subset=['state'])






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 27 at 15:23









          jezraeljezrael

          401k29 gold badges412 silver badges479 bronze badges




          401k29 gold badges412 silver badges479 bronze badges


























              0














              It's a bit hack-y, but a simpler solution might take a form similar to:



              for row in dataRows:
              for state in state_search:
              if state in row:
              #put state in correct column here
              break #should break just the inner loop; if that doesn't happen, delete this line


              It's probably helpful to think about how the underlying program would have to approach the problem (checking each row for a string that matches one of your states, then doing something with it), and go at that directly. Unless you're dealing with a huge load of data, it may not be worth going crazy fancy with regular expressions or the like.






              share|improve this answer































                0














                It's a bit hack-y, but a simpler solution might take a form similar to:



                for row in dataRows:
                for state in state_search:
                if state in row:
                #put state in correct column here
                break #should break just the inner loop; if that doesn't happen, delete this line


                It's probably helpful to think about how the underlying program would have to approach the problem (checking each row for a string that matches one of your states, then doing something with it), and go at that directly. Unless you're dealing with a huge load of data, it may not be worth going crazy fancy with regular expressions or the like.






                share|improve this answer





























                  0












                  0








                  0







                  It's a bit hack-y, but a simpler solution might take a form similar to:



                  for row in dataRows:
                  for state in state_search:
                  if state in row:
                  #put state in correct column here
                  break #should break just the inner loop; if that doesn't happen, delete this line


                  It's probably helpful to think about how the underlying program would have to approach the problem (checking each row for a string that matches one of your states, then doing something with it), and go at that directly. Unless you're dealing with a huge load of data, it may not be worth going crazy fancy with regular expressions or the like.






                  share|improve this answer















                  It's a bit hack-y, but a simpler solution might take a form similar to:



                  for row in dataRows:
                  for state in state_search:
                  if state in row:
                  #put state in correct column here
                  break #should break just the inner loop; if that doesn't happen, delete this line


                  It's probably helpful to think about how the underlying program would have to approach the problem (checking each row for a string that matches one of your states, then doing something with it), and go at that directly. Unless you're dealing with a huge load of data, it may not be worth going crazy fancy with regular expressions or the like.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 27 at 15:16

























                  answered Mar 27 at 15:10









                  Taylor NelmsTaylor Nelms

                  2252 silver badges14 bronze badges




                  2252 silver badges14 bronze badges






























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55380454%2fsearching-for-an-item-within-a-list-in-a-column-and-saving-that-item-to-a-new-co%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







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript