Split and map original values to different pandas column
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I would like to map a function that splits names with the help of the nameparser
package in python.
The function I use is the following:
def extract_parts(name):
first, middle, last = (HumanName(name)).first,(HumanName(name)).middle, (HumanName(name)).last
return first, middle, last
And the dataframe would be like the following for example with the NAMES
column that contains the names that need to be split with the nameparser
package.
ID | NAMES | column
1 | Ben Jerry | I
2 | John Jack Joe | I
3 | Dr. Amelia von Lugenwitz | I
4 | Cristian Maria de Angel | I
5 | The CBA Company | C
6 | FBPTQ | C
And I loop the function and try to map it, but it only returns the last value. I guess it makes sense. But .apply()
did not work, or I don't know how to apply it properly.
for index in range(len(file)):
if file["column"][index] == "A":
try:
file["COLUMN1"], file["COLUMN2"], file["COLUMN3"] = extract_parts(file["ORIGINAL"][index])
except TypeError as e:
print(e)
And it should return a dataframe:
ID | COLUMN1 | COLUMN2 | COLUMN3
1 | Ben | | Jerry
2 | John | Jack | Joe
3 | Amelia | | von Lugenwitz
4 | Cristian| Maria | de Angel
Whereby COLUMN1
represent the First name that is extracted by nameparser HumanName(), COLUMN2
the Middle Names and COLUMN3
the Last name.
python pandas
|
show 2 more comments
I would like to map a function that splits names with the help of the nameparser
package in python.
The function I use is the following:
def extract_parts(name):
first, middle, last = (HumanName(name)).first,(HumanName(name)).middle, (HumanName(name)).last
return first, middle, last
And the dataframe would be like the following for example with the NAMES
column that contains the names that need to be split with the nameparser
package.
ID | NAMES | column
1 | Ben Jerry | I
2 | John Jack Joe | I
3 | Dr. Amelia von Lugenwitz | I
4 | Cristian Maria de Angel | I
5 | The CBA Company | C
6 | FBPTQ | C
And I loop the function and try to map it, but it only returns the last value. I guess it makes sense. But .apply()
did not work, or I don't know how to apply it properly.
for index in range(len(file)):
if file["column"][index] == "A":
try:
file["COLUMN1"], file["COLUMN2"], file["COLUMN3"] = extract_parts(file["ORIGINAL"][index])
except TypeError as e:
print(e)
And it should return a dataframe:
ID | COLUMN1 | COLUMN2 | COLUMN3
1 | Ben | | Jerry
2 | John | Jack | Joe
3 | Amelia | | von Lugenwitz
4 | Cristian| Maria | de Angel
Whereby COLUMN1
represent the First name that is extracted by nameparser HumanName(), COLUMN2
the Middle Names and COLUMN3
the Last name.
python pandas
Please share how are the names stored. ADataFrame
?
– yatu
Jan 3 at 9:57
Yes, I have added that they are stored in a Pandas column
– mrPy
Jan 3 at 10:00
@yatu that is what the function extract parts do
– Daniel Mesejo
Jan 3 at 10:03
I believe the name_parser take a guess and leave one of the two empty
– Daniel Mesejo
Jan 3 at 10:05
the nameparser would know what the first and last name is. You can try it out yatu..
– mrPy
Jan 3 at 10:06
|
show 2 more comments
I would like to map a function that splits names with the help of the nameparser
package in python.
The function I use is the following:
def extract_parts(name):
first, middle, last = (HumanName(name)).first,(HumanName(name)).middle, (HumanName(name)).last
return first, middle, last
And the dataframe would be like the following for example with the NAMES
column that contains the names that need to be split with the nameparser
package.
ID | NAMES | column
1 | Ben Jerry | I
2 | John Jack Joe | I
3 | Dr. Amelia von Lugenwitz | I
4 | Cristian Maria de Angel | I
5 | The CBA Company | C
6 | FBPTQ | C
And I loop the function and try to map it, but it only returns the last value. I guess it makes sense. But .apply()
did not work, or I don't know how to apply it properly.
for index in range(len(file)):
if file["column"][index] == "A":
try:
file["COLUMN1"], file["COLUMN2"], file["COLUMN3"] = extract_parts(file["ORIGINAL"][index])
except TypeError as e:
print(e)
And it should return a dataframe:
ID | COLUMN1 | COLUMN2 | COLUMN3
1 | Ben | | Jerry
2 | John | Jack | Joe
3 | Amelia | | von Lugenwitz
4 | Cristian| Maria | de Angel
Whereby COLUMN1
represent the First name that is extracted by nameparser HumanName(), COLUMN2
the Middle Names and COLUMN3
the Last name.
python pandas
I would like to map a function that splits names with the help of the nameparser
package in python.
The function I use is the following:
def extract_parts(name):
first, middle, last = (HumanName(name)).first,(HumanName(name)).middle, (HumanName(name)).last
return first, middle, last
And the dataframe would be like the following for example with the NAMES
column that contains the names that need to be split with the nameparser
package.
ID | NAMES | column
1 | Ben Jerry | I
2 | John Jack Joe | I
3 | Dr. Amelia von Lugenwitz | I
4 | Cristian Maria de Angel | I
5 | The CBA Company | C
6 | FBPTQ | C
And I loop the function and try to map it, but it only returns the last value. I guess it makes sense. But .apply()
did not work, or I don't know how to apply it properly.
for index in range(len(file)):
if file["column"][index] == "A":
try:
file["COLUMN1"], file["COLUMN2"], file["COLUMN3"] = extract_parts(file["ORIGINAL"][index])
except TypeError as e:
print(e)
And it should return a dataframe:
ID | COLUMN1 | COLUMN2 | COLUMN3
1 | Ben | | Jerry
2 | John | Jack | Joe
3 | Amelia | | von Lugenwitz
4 | Cristian| Maria | de Angel
Whereby COLUMN1
represent the First name that is extracted by nameparser HumanName(), COLUMN2
the Middle Names and COLUMN3
the Last name.
python pandas
python pandas
edited Jan 3 at 12:21


Daniel Mesejo
18.8k21533
18.8k21533
asked Jan 3 at 9:52
mrPymrPy
857
857
Please share how are the names stored. ADataFrame
?
– yatu
Jan 3 at 9:57
Yes, I have added that they are stored in a Pandas column
– mrPy
Jan 3 at 10:00
@yatu that is what the function extract parts do
– Daniel Mesejo
Jan 3 at 10:03
I believe the name_parser take a guess and leave one of the two empty
– Daniel Mesejo
Jan 3 at 10:05
the nameparser would know what the first and last name is. You can try it out yatu..
– mrPy
Jan 3 at 10:06
|
show 2 more comments
Please share how are the names stored. ADataFrame
?
– yatu
Jan 3 at 9:57
Yes, I have added that they are stored in a Pandas column
– mrPy
Jan 3 at 10:00
@yatu that is what the function extract parts do
– Daniel Mesejo
Jan 3 at 10:03
I believe the name_parser take a guess and leave one of the two empty
– Daniel Mesejo
Jan 3 at 10:05
the nameparser would know what the first and last name is. You can try it out yatu..
– mrPy
Jan 3 at 10:06
Please share how are the names stored. A
DataFrame
?– yatu
Jan 3 at 9:57
Please share how are the names stored. A
DataFrame
?– yatu
Jan 3 at 9:57
Yes, I have added that they are stored in a Pandas column
– mrPy
Jan 3 at 10:00
Yes, I have added that they are stored in a Pandas column
– mrPy
Jan 3 at 10:00
@yatu that is what the function extract parts do
– Daniel Mesejo
Jan 3 at 10:03
@yatu that is what the function extract parts do
– Daniel Mesejo
Jan 3 at 10:03
I believe the name_parser take a guess and leave one of the two empty
– Daniel Mesejo
Jan 3 at 10:05
I believe the name_parser take a guess and leave one of the two empty
– Daniel Mesejo
Jan 3 at 10:05
the nameparser would know what the first and last name is. You can try it out yatu..
– mrPy
Jan 3 at 10:06
the nameparser would know what the first and last name is. You can try it out yatu..
– mrPy
Jan 3 at 10:06
|
show 2 more comments
1 Answer
1
active
oldest
votes
Assuming your dataframe has a names
column, for example:
names
0 Ben Jerry
1 John Jack Joe
2 Dr. Amelia von Lugenwitz
3 Cristian Maria de Angel
You could use zip to unpack the values into multiple columns:
df['first'], df['middle'], df['last'] = zip(*df['names'].apply(extract_parts))
Do I use this in the for loop instead of what I have after the try statement? Would I do something with the index also?
– mrPy
Jan 3 at 10:05
No need to use afor
loop. Just do the line above one-time, changedf
to the name of your dataframe
– Daniel Mesejo
Jan 3 at 10:06
Doesn't work. You didn't take into account the if statement, so I get the following error:TypeError: expected string or bytes-like object
. And when I use a try-except, it doesn't create the columns
– mrPy
Jan 3 at 12:13
@mrPy It will be better if you add a full example on your question, I mean a small sample of your original dataframe
– Daniel Mesejo
Jan 3 at 12:14
@mrPy You haveif file["column"][index] == "A"
what is the relation with the "names" column?
– Daniel Mesejo
Jan 3 at 12:18
|
show 1 more comment
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
});
}
});
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%2f54019847%2fsplit-and-map-original-values-to-different-pandas-column%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming your dataframe has a names
column, for example:
names
0 Ben Jerry
1 John Jack Joe
2 Dr. Amelia von Lugenwitz
3 Cristian Maria de Angel
You could use zip to unpack the values into multiple columns:
df['first'], df['middle'], df['last'] = zip(*df['names'].apply(extract_parts))
Do I use this in the for loop instead of what I have after the try statement? Would I do something with the index also?
– mrPy
Jan 3 at 10:05
No need to use afor
loop. Just do the line above one-time, changedf
to the name of your dataframe
– Daniel Mesejo
Jan 3 at 10:06
Doesn't work. You didn't take into account the if statement, so I get the following error:TypeError: expected string or bytes-like object
. And when I use a try-except, it doesn't create the columns
– mrPy
Jan 3 at 12:13
@mrPy It will be better if you add a full example on your question, I mean a small sample of your original dataframe
– Daniel Mesejo
Jan 3 at 12:14
@mrPy You haveif file["column"][index] == "A"
what is the relation with the "names" column?
– Daniel Mesejo
Jan 3 at 12:18
|
show 1 more comment
Assuming your dataframe has a names
column, for example:
names
0 Ben Jerry
1 John Jack Joe
2 Dr. Amelia von Lugenwitz
3 Cristian Maria de Angel
You could use zip to unpack the values into multiple columns:
df['first'], df['middle'], df['last'] = zip(*df['names'].apply(extract_parts))
Do I use this in the for loop instead of what I have after the try statement? Would I do something with the index also?
– mrPy
Jan 3 at 10:05
No need to use afor
loop. Just do the line above one-time, changedf
to the name of your dataframe
– Daniel Mesejo
Jan 3 at 10:06
Doesn't work. You didn't take into account the if statement, so I get the following error:TypeError: expected string or bytes-like object
. And when I use a try-except, it doesn't create the columns
– mrPy
Jan 3 at 12:13
@mrPy It will be better if you add a full example on your question, I mean a small sample of your original dataframe
– Daniel Mesejo
Jan 3 at 12:14
@mrPy You haveif file["column"][index] == "A"
what is the relation with the "names" column?
– Daniel Mesejo
Jan 3 at 12:18
|
show 1 more comment
Assuming your dataframe has a names
column, for example:
names
0 Ben Jerry
1 John Jack Joe
2 Dr. Amelia von Lugenwitz
3 Cristian Maria de Angel
You could use zip to unpack the values into multiple columns:
df['first'], df['middle'], df['last'] = zip(*df['names'].apply(extract_parts))
Assuming your dataframe has a names
column, for example:
names
0 Ben Jerry
1 John Jack Joe
2 Dr. Amelia von Lugenwitz
3 Cristian Maria de Angel
You could use zip to unpack the values into multiple columns:
df['first'], df['middle'], df['last'] = zip(*df['names'].apply(extract_parts))
edited Jan 3 at 10:06
answered Jan 3 at 9:59


Daniel MesejoDaniel Mesejo
18.8k21533
18.8k21533
Do I use this in the for loop instead of what I have after the try statement? Would I do something with the index also?
– mrPy
Jan 3 at 10:05
No need to use afor
loop. Just do the line above one-time, changedf
to the name of your dataframe
– Daniel Mesejo
Jan 3 at 10:06
Doesn't work. You didn't take into account the if statement, so I get the following error:TypeError: expected string or bytes-like object
. And when I use a try-except, it doesn't create the columns
– mrPy
Jan 3 at 12:13
@mrPy It will be better if you add a full example on your question, I mean a small sample of your original dataframe
– Daniel Mesejo
Jan 3 at 12:14
@mrPy You haveif file["column"][index] == "A"
what is the relation with the "names" column?
– Daniel Mesejo
Jan 3 at 12:18
|
show 1 more comment
Do I use this in the for loop instead of what I have after the try statement? Would I do something with the index also?
– mrPy
Jan 3 at 10:05
No need to use afor
loop. Just do the line above one-time, changedf
to the name of your dataframe
– Daniel Mesejo
Jan 3 at 10:06
Doesn't work. You didn't take into account the if statement, so I get the following error:TypeError: expected string or bytes-like object
. And when I use a try-except, it doesn't create the columns
– mrPy
Jan 3 at 12:13
@mrPy It will be better if you add a full example on your question, I mean a small sample of your original dataframe
– Daniel Mesejo
Jan 3 at 12:14
@mrPy You haveif file["column"][index] == "A"
what is the relation with the "names" column?
– Daniel Mesejo
Jan 3 at 12:18
Do I use this in the for loop instead of what I have after the try statement? Would I do something with the index also?
– mrPy
Jan 3 at 10:05
Do I use this in the for loop instead of what I have after the try statement? Would I do something with the index also?
– mrPy
Jan 3 at 10:05
No need to use a
for
loop. Just do the line above one-time, change df
to the name of your dataframe– Daniel Mesejo
Jan 3 at 10:06
No need to use a
for
loop. Just do the line above one-time, change df
to the name of your dataframe– Daniel Mesejo
Jan 3 at 10:06
Doesn't work. You didn't take into account the if statement, so I get the following error:
TypeError: expected string or bytes-like object
. And when I use a try-except, it doesn't create the columns– mrPy
Jan 3 at 12:13
Doesn't work. You didn't take into account the if statement, so I get the following error:
TypeError: expected string or bytes-like object
. And when I use a try-except, it doesn't create the columns– mrPy
Jan 3 at 12:13
@mrPy It will be better if you add a full example on your question, I mean a small sample of your original dataframe
– Daniel Mesejo
Jan 3 at 12:14
@mrPy It will be better if you add a full example on your question, I mean a small sample of your original dataframe
– Daniel Mesejo
Jan 3 at 12:14
@mrPy You have
if file["column"][index] == "A"
what is the relation with the "names" column?– Daniel Mesejo
Jan 3 at 12:18
@mrPy You have
if file["column"][index] == "A"
what is the relation with the "names" column?– Daniel Mesejo
Jan 3 at 12:18
|
show 1 more comment
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.
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%2f54019847%2fsplit-and-map-original-values-to-different-pandas-column%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
Please share how are the names stored. A
DataFrame
?– yatu
Jan 3 at 9:57
Yes, I have added that they are stored in a Pandas column
– mrPy
Jan 3 at 10:00
@yatu that is what the function extract parts do
– Daniel Mesejo
Jan 3 at 10:03
I believe the name_parser take a guess and leave one of the two empty
– Daniel Mesejo
Jan 3 at 10:05
the nameparser would know what the first and last name is. You can try it out yatu..
– mrPy
Jan 3 at 10:06