0

I have the following code

df2['TaxAccNo4'] = df2['TaxAccNo2'].apply(lambda x: x.split('.')[0])
df2['TaxAccNo3'] = df2['TaxAccNo2'].apply(lambda x: x.split('.')[1])

where df2 is:

     TaxAccNo2    
0    00001379.1   
1    00182218    

When I run the code I get

     TaxAccNo2   TaxAccNo4
0    00001379.1  00001379
1    00182218    00182218

and IndexError: list index out of range for TaxAccNo3,

     TaxAccNo2   TaxAccNo4   TaxAccNo3
0    00001379.1  00001379    1
1    00182218    00182218    

How do I fix my code to produce that output? I'm assuming its giving me the error because Index 1 doesn't have '.' but I'm not sure how to fix that.

2 Answers 2

1

Hy, I was reviewing your code, the problem is that when you use the method split() in a string the returned object is a list, and this is causing the index error, as you pointed. The solution I encountered if very simple, use a conditional in your code to prevent it from calling this index for shorter lists as follows. Hope it helps.

df2['TaxAccNo3'] = df2['TaxAccNo2'].apply(lambda x: x.split('.')[1] if len(x.split('.'))>1 else x)
Sign up to request clarification or add additional context in comments.

Comments

0

As you said, the problem is that "00182218".split(".") doesn't have a [1] index, since it's the list ["00182218"].

A simple solution without affecting too much the code is to use a ... if ... else ... ternary operator:

df2['TaxAccNo4'] = df2['TaxAccNo2'].apply(lambda x: x.split('.')[0])
df2['TaxAccNo3'] = df2['TaxAccNo2'].apply(lambda x: x.split('.')[1] if '.' in x else '')

Where the last '' is an empty string, the value with which you will fill 'TaxAccNo3' if 'TaxAccNo2' doesn't have a dot (you can replace it if you want other behaviour).

The semantic is: put x.split('.')[1] in df2['TaxAccNo3'] if x contains a dot, otherwise put an empty string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.