473,324 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

multiple conditional substring function

Hi There,

I am new to python so please be kind to me.
I am learning the substring function, so I am just trying different cases to get familiar.

I have managed to get the output for a single substring function. However when I apply multiple conditions then I get the following error. Appreciate if you guys educate me on this.

Expand|Select|Wrap|Line Numbers
  1. data = {'name': ['John', 'Aaron', 'Anie', 'Nancy', 'Steve'], 
  2.         'Gender': ['00M00','00M00','00F00','00F00','00x00'], 
  3.         'Dept': ['01MK00', '02FN00', '03LG00', '04HR00', '05DR00']}
  4. df = pd.DataFrame(data, columns = ['name', 'Gender', 'Dept'])
  5. df
  6.  
  7.  
  8. var=[]
  9.  
  10. for i in df["Gender"]:
  11. for x in df["Dept"]:
  12.  
  13.     if i[2].lower()=='m' & x[2:4].lower()=='mk':
  14.         var.append('Male in Marketing')
  15.  
  16.     elif i[2].lower()=='f' & x[2:4].lower()=='fn':
  17.         var.append('Female in Finance')
  18.  
  19.     else:
  20.         var.append('Others')
  21.  
  22. Error message below
  23.   File "<ipython-input-79-ff06a7e562be>", line 4
  24.     for x in df["Dept"]:
  25.       ^
  26. IndentationError: expected an indented block
  27.  
  28.  
Regards,
CK
Jul 8 '20 #1

✓ answered by SioSio

It can use the built-in function zip() to get the values ​​of multiple columns at once.

Expand|Select|Wrap|Line Numbers
  1. for Gender, Dept in zip(df['Gender'], df['Dept']):
  2.     if Gender[2].lower() in 'm' and Dept[2:4].lower() in 'mk':
  3.         var.append('Male in Marketing')
  4.     elif Gender[2].lower()in 'f' and Dept[2:4].lower() in 'fn':
  5.         var.append('Female in Finance')
  6.     else:
  7.         var.append('Others')
  8.  

7 3276
SioSio
272 256MB
If you just want to fix the error in this code:
Expand|Select|Wrap|Line Numbers
  1. import pandas as pd
  2.  
  3. data = {'name': ['John', 'Aaron', 'Anie', 'Nancy', 'Steve'],
  4.         'Gender': ['00M00','00M00','00F00','00F00','00x00'],
  5.         'Dept': ['01MK00', '02FN00', '03LG00', '04HR00', '05DR00']}
  6. df = pd.DataFrame(data, columns = ['name', 'Gender', 'Dept'])
  7. df
  8.  
  9.  
  10. var=[]
  11.  
  12. for i in df["Gender"]:
  13.     for x in df["Dept"]:
  14.  
  15.         if i[2].lower() in 'm' and x[2:4].lower() in 'mk':
  16.             var.append('Male in Marketing')
  17.         elif i[2].lower()in 'f' and x[2:4].lower() in 'fn':
  18.             var.append('Female in Finance')
  19.         else:
  20.             var.append('Others')
  21.  
Jul 8 '20 #2
Hi There,

Thanks for this,

However, still, I am getting the following error after running the above code:

Is there any better way to enhance the code to get the right output.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. var=[]
  4.  
  5. for i in df["Gender"]:
  6.     for x in df["Dept"]:
  7.  
  8.         if i[2].lower() in 'm' and x[2:4].lower() in 'mk':
  9.             var.append('Male in Marketing')
  10.         elif i[2].lower()in 'f' and x[2:4].lower() in 'fn':
  11.             var.append('Female in Finance')
  12.         else:
  13.             var.append('Others')
  14.  
  15. df["new_col"]=var
  16. df.head()
  17.  
  18. Error message below
  19.  
  20.  
  21. ValueError                                Traceback (most recent call last)
  22. <ipython-input-93-dd3e254bfbaf> in <module>
  23. ----> 1 df["new_col"]=var
  24.       2 df.head(5)
  25.  
  26. H:\Softwares\PythonSoftware\lib\site-packages\pandas\core\frame.py in __setitem__(self, key, value)
  27.    2936         else:
  28.    2937             # set column
  29. -> 2938             self._set_item(key, value)
  30.    2939 
  31.    2940     def _setitem_slice(self, key, value):
  32.  
  33. H:\Softwares\PythonSoftware\lib\site-packages\pandas\core\frame.py in _set_item(self, key, value)
  34.    2998 
  35.    2999         self._ensure_valid_index(value)
  36. -> 3000         value = self._sanitize_column(key, value)
  37.    3001         NDFrame._set_item(self, key, value)
  38.    3002 
  39.  
  40. H:\Softwares\PythonSoftware\lib\site-packages\pandas\core\frame.py in _sanitize_column(self, key, value, broadcast)
  41.    3634 
  42.    3635             # turn me into an ndarray
  43. -> 3636             value = sanitize_index(value, self.index, copy=False)
  44.    3637             if not isinstance(value, (np.ndarray, Index)):
  45.    3638                 if isinstance(value, list) and len(value) > 0:
  46.  
  47. H:\Softwares\PythonSoftware\lib\site-packages\pandas\core\internals\construction.py in sanitize_index(data, index, copy)
  48.     609 
  49.     610     if len(data) != len(index):
  50. --> 611         raise ValueError("Length of values does not match length of index")
  51.     612 
  52.     613     if isinstance(data, ABCIndexClass) and not copy:
  53.  
  54. ValueError: Length of values does not match length of index
  55.  
Jul 8 '20 #3
SioSio
272 256MB
Error Message: "Value length does not match index length"

The array size of df is 5, but var is 5x5 = 25.
Jul 8 '20 #4
Hi There,

Is there any workaround to satisfy the above condition.
Jul 8 '20 #5
SioSio
272 256MB
It can use the built-in function zip() to get the values ​​of multiple columns at once.

Expand|Select|Wrap|Line Numbers
  1. for Gender, Dept in zip(df['Gender'], df['Dept']):
  2.     if Gender[2].lower() in 'm' and Dept[2:4].lower() in 'mk':
  3.         var.append('Male in Marketing')
  4.     elif Gender[2].lower()in 'f' and Dept[2:4].lower() in 'fn':
  5.         var.append('Female in Finance')
  6.     else:
  7.         var.append('Others')
  8.  
Jul 8 '20 #6
HI SioSio,

Thanks for the advice and help with this.

Kind regards,
CK
Jul 8 '20 #7
markelvy
1 Bit
The ValueError: Length of values does not match length of index raised because the previous columns you have added in the DataFrame are not the same length as the most recent one you have attempted to add in the DataFrame. So, you need make sure that the length of the array you are assign to a new column is equal to the length of the dataframe .

The simple solution is that you first convert the list/array to a pandas Series , and then when you do assignment, missing index in the Series will be filled with NaN values .

Expand|Select|Wrap|Line Numbers
  1. df = pd.DataFrame({'X': [1,2,3,4]})
  2. df['Y'] = pd.Series([3,4])
Jul 5 '21 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Radhika Sambamurti | last post by:
Hi, I've written a substring function. The prototype is: int substr(char s1, char s2) Returns 1 if s2 is a substring of s1, else it returns 0. I have written this program, but Im sure there is an...
1
by: ammarton | last post by:
Hello all...I'm a bit new to working with Macros in Access so forgive me if the terminology I use is not accurate. To preface this, basically I am using a form on a replicated database so the...
2
by: booksnore | last post by:
..eh I was stuck thinking up a subject title for this post for a while.. So I am processing a really big file (scary big). Each record is fixed length, I need to test conditions on certain fields...
5
by: Shailja | last post by:
Kindly tell me how to use Substring function in VB.
3
by: Rocky86 | last post by:
anybody know how to do a substring function in actionscript pls help~!! I want to do something like extact the number out and convert it to string!
1
by: ashokbio | last post by:
How to return multiple values by passing multiple arguments in function or subroutine using VB6? T. Ashok Kumar
0
by: ponvijaya | last post by:
Hi all, I have used a substring function inside my stored procedre as set @dummy1=substring(@dummyid,2); but when i print and check the value of @dummy1 as select @dummy1;
1
by: obanite | last post by:
Hello, Is there a better way of doing: <xsl:variable name="frlink"> <xsl:choose> <xsl:when test="@isFriend = '0'"><xsl:value-of select="concat('friends.php?add=', @uid)"/></xsl:when> ...
2
by: pnunbe | last post by:
Hi, I have a problem with the 'substring' function of XPath in VB.NET (VS 2005). My code looks like this: dim Dom As new XmlDocument Dim mgr As New XmlNamespaceManager(Dom.NameTable)...
4
by: rhuseman | last post by:
On my form I have 24 combo boxes ( 12 of which are conditional/cascading combo boxes dependent on the users input of the other 12 combo boxes). I've found ways to do it by code each individual...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.