pandas dataframe apply lambda index error
up vote
0
down vote
favorite
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.
python pandas dataframe
add a comment |
up vote
0
down vote
favorite
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.
python pandas dataframe
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
python pandas dataframe
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.
python pandas dataframe
python pandas dataframe
edited Nov 21 at 1:11
eyllanesc
68.6k93052
68.6k93052
asked Nov 21 at 1:09
Andy
275
275
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
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.
New contributor
add a comment |
up vote
0
down vote
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)
New contributor
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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.
New contributor
add a comment |
up vote
0
down vote
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.
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
New contributor
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.
New contributor
New contributor
answered Nov 21 at 1:19
Julian Peller
844411
844411
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
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)
New contributor
add a comment |
up vote
0
down vote
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)
New contributor
add a comment |
up vote
0
down vote
up vote
0
down vote
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)
New contributor
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)
New contributor
New contributor
answered Nov 21 at 1:51
Pedro Moresco
113
113
New contributor
New contributor
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403917%2fpandas-dataframe-apply-lambda-index-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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