Home > Software design >  Looking for a certain phrase in pandas column
Looking for a certain phrase in pandas column

Time:09-21

I am trying to look for a certain phrase in pandas column using this code:

workfromhome = df.loc[df['Description'].str.contains("work from home",na=False)]
df['Work From Home'] = workfromhome

here is the description column: About AgodaAgoda is an online travel booking p...

1       DescriptionWe are looking for an experienced B...
2       Key Responsibilities and Accountabilities To w...
3       About AgodaAgoda is an online travel booking p...
4       Data entry InternshipResponsible for adding ne...
5       Siemens EDA is a global technology powerhouse....
6       Job DescriptionThis is a remote position.Excep...
7       Curenta is changing the shape of long-term hea...
8       Job RequirementsBachelors Degree in Computer S...
9       Where You’ll Work Andela is a network of techn...
10      QualificationsPostgraduate, master’s degree or...
11      متاح طوال ايام العمل يقبل العمل بعد فترة الدوا...
12      About The JobYou’re looking for fulfillment, w...
13      About AgodaAgoda is an online travel booking p...
14      Ready to tackle the challenges of the vehicle ...
15      About AgodaAgoda is an online travel booking p...
16      Siemens EDA is a global technology powerhouse....
17      About AgodaAgoda is an online travel booking p...
18      IGNITION is a 12 months program designed to gi...
19      Management Trainee ProgramRotational program f...
20      Siemens EDA is a global technology powerhouse....
21      Siemens EDA is a global technology powerhouse....
22      Votre Mission Chez CegedimWe are looking for a...
23      About AgodaAgoda is an online travel booking p...
24      Votre Mission Chez CegedimWe are looking for a...
25      RequirementsBachelor's degree in Computer Scie...
26      DescriptionWe are looking for an iOS developer...
27      Designs, develops and executes all testing-rel...
28      About AgodaAgoda is an online travel booking p...
29      Auto req ID: 247864BRJob DescriptionDevelop an...
30      Job DescriptionABOUT THIS JOB A Reporting Offi...
31      About AgodaAgoda is an online travel booking p...
32      Job DescriptionProvide technical mentor ship a...
33      About AgodaAgoda is an online travel booking p...
34      Job briefWe are looking for a Java Developer w...
35      About The JobJob DescriptionTo be responsible ...
36      يكون لدية الخبرة فى ادارة وتطوير الشركات Show ...
37      Job ScopeThe job scope covers the execution of...
38      About GemographyJoin Gemography and work direc...
39      About This JobJob DescriptionA Reporting Offic...
40      Job DescriptionVotre mission chez Cegedim :Ove...
41      Essential Job FunctionsResponsibilitiesContrib...
42      DescriptionWe are looking for a Full Stack Dev...
43      DescriptionWe are looking for a passionate Dat...
44      Auto req ID: 248881BRRoleJob DescriptionThe HR...
45      1 Bachelors or diploma in computer engineering...
46      DescriptionWhat are we looking for?We are look...
47      Requirements• Development experience of 8 year...
48      DescriptionWe are looking for Devops engineers...
49      DescriptionSouq.com, an Amazon.com Company, is...
50      Siemens EDA is a global technology powerhouse....
51      ResponsibilitiesParticipates as a member of a ...
52      About AgodaAgoda is an online travel booking p...
53      About AgodaAgoda is an online travel booking p...
54      The ideal candidate will be responsible for cr...
55      DescriptionOLX is on the hunt for an experienc...
56      Siemens EDA is a global technology powerhouse....
57      We are hiring a Senior Backend Developer who m...
58      Company Name:Elsewedy Electric - ISKRAEMECODep...
59      DescriptionSouq.com, an Amazon.com Company, is...
60      Votre Mission Chez CegedimAs a Tester you will...

now when I was looking for the social insurance using the same exact code! it worked no problem! but now when I look for medical insurance or work from home! it returns this error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Work From Home'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in set(self, item, value)
   1070         try:
-> 1071             loc = self.items.get_loc(item)
   1072         except KeyError:

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Work From Home'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-448-4f0e407389ac> in <module>
      1 #df['Social Insurance'] = df.loc[df['Description'].str.contains("social insurance", case=False,na=False)]
      2 workfromhome = df.loc[df['Description'].str.contains("work from home",na=False)]
----> 3 df['Work From Home'] = workfromhome

~\anaconda3\lib\site-packages\pandas\core\frame.py in __setitem__(self, key, value)
   2936         else:
   2937             # set column
-> 2938             self._set_item(key, value)
   2939 
   2940     def _setitem_slice(self, key, value):

~\anaconda3\lib\site-packages\pandas\core\frame.py in _set_item(self, key, value)
   2999         self._ensure_valid_index(value)
   3000         value = self._sanitize_column(key, value)
-> 3001         NDFrame._set_item(self, key, value)
   3002 
   3003         # check if we are modifying a copy

~\anaconda3\lib\site-packages\pandas\core\generic.py in _set_item(self, key, value)
   3622 
   3623     def _set_item(self, key, value) -> None:
-> 3624         self._data.set(key, value)
   3625         self._clear_item_cache()
   3626 

~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in set(self, item, value)
   1072         except KeyError:
   1073             # This item wasn't present, just insert at end
-> 1074             self.insert(len(self.items), item, value)
   1075             return
   1076 

~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in insert(self, loc, item, value, allow_duplicates)
   1179         new_axis = self.items.insert(loc, item)
   1180 
-> 1181         block = make_block(values=value, ndim=self.ndim, placement=slice(loc, loc   1))
   1182 
   1183         for blkno, count in _fast_count_smallints(self._blknos[loc:]):

~\anaconda3\lib\site-packages\pandas\core\internals\blocks.py in make_block(values, placement, klass, ndim, dtype)
   3051         values = DatetimeArray._simple_new(values, dtype=dtype)
   3052 
-> 3053     return klass(values, ndim=ndim, placement=placement)
   3054 
   3055 

~\anaconda3\lib\site-packages\pandas\core\internals\blocks.py in __init__(self, values, placement, ndim)
   2599             values = np.array(values, dtype=object)
   2600 
-> 2601         super().__init__(values, ndim=ndim, placement=placement)
   2602 
   2603     @property

~\anaconda3\lib\site-packages\pandas\core\internals\blocks.py in __init__(self, values, placement, ndim)
    122 
    123         if self._validate_ndim and self.ndim and len(self.mgr_locs) != len(self.values):
--> 124             raise ValueError(
    125                 f"Wrong number of items passed {len(self.values)}, "
    126                 f"placement implies {len(self.mgr_locs)}"

ValueError: Wrong number of items passed 21, placement implies 1

Help me extract the information please

CodePudding user response:

Try define column Description for return Series in variable workfromhome:

workfromhome = df.loc[df['Description'].str.contains("work from home",na=False), 'Description']
  • Related