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

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현