Using a list as a string of the name and the elements within the list
Is there a way to make a list of lists and then loop through them.
Essentially I need to use the elements of multiple lists as a condition but also use the name of the as a string / column name.
I Know the example below can be done in a easier way, but I think it needs this approach as my task is a bit more complex than the below
df=
name
0 Alice
1 Fred
2 George
male=['fred','george']
female=['alice','emily']
alllists=[male, female]
for i in alllists:
df[i]=0
df.loc[df['Name'].str.contains('|'.join(i),na=False),l]=1
Output df
name Male Female
0 Alice 0 1
1 Fred 1 0
2 George 1 0
python string pandas list loops
add a comment |
Is there a way to make a list of lists and then loop through them.
Essentially I need to use the elements of multiple lists as a condition but also use the name of the as a string / column name.
I Know the example below can be done in a easier way, but I think it needs this approach as my task is a bit more complex than the below
df=
name
0 Alice
1 Fred
2 George
male=['fred','george']
female=['alice','emily']
alllists=[male, female]
for i in alllists:
df[i]=0
df.loc[df['Name'].str.contains('|'.join(i),na=False),l]=1
Output df
name Male Female
0 Alice 0 1
1 Fred 1 0
2 George 1 0
python string pandas list loops
add a comment |
Is there a way to make a list of lists and then loop through them.
Essentially I need to use the elements of multiple lists as a condition but also use the name of the as a string / column name.
I Know the example below can be done in a easier way, but I think it needs this approach as my task is a bit more complex than the below
df=
name
0 Alice
1 Fred
2 George
male=['fred','george']
female=['alice','emily']
alllists=[male, female]
for i in alllists:
df[i]=0
df.loc[df['Name'].str.contains('|'.join(i),na=False),l]=1
Output df
name Male Female
0 Alice 0 1
1 Fred 1 0
2 George 1 0
python string pandas list loops
Is there a way to make a list of lists and then loop through them.
Essentially I need to use the elements of multiple lists as a condition but also use the name of the as a string / column name.
I Know the example below can be done in a easier way, but I think it needs this approach as my task is a bit more complex than the below
df=
name
0 Alice
1 Fred
2 George
male=['fred','george']
female=['alice','emily']
alllists=[male, female]
for i in alllists:
df[i]=0
df.loc[df['Name'].str.contains('|'.join(i),na=False),l]=1
Output df
name Male Female
0 Alice 0 1
1 Fred 1 0
2 George 1 0
python string pandas list loops
python string pandas list loops
edited Nov 22 at 14:54
jpp
90.5k2052101
90.5k2052101
asked Nov 22 at 14:47
fred.schwartz
2958
2958
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
While there is a way, it's not recommended. Just use a dictionary:
d = {'male': ['fred', 'george'],
'female': ['alice', 'emily']}
for k, v in d.items():
mask = df['name'].str.lower().str.contains('|'.join(v), na=False)
df[k.capitalize()] = mask.astype(int)
mask.astype(int)
works because a Boolean array may be mapped directly to 1
/ 0
, just as bool
is a subclass of int
in regular Python.
Result:
print(df)
name Male Female
0 Alice 0 1
1 Fred 1 0
2 George 1 0
1
Thanks alot this worked very nicely
– fred.schwartz
Nov 22 at 16:35
add a comment |
You can use pandas.get_dummies
.
>>> males = {'fred', 'george'}
>>> fm = pd.get_dummies(['Male' if name.lower() in males else 'Female' for name in df['name']])
>>> result = pd.concat([df, fm], axis=1)
>>>
>>> result
name Female Male
0 Alice 1 0
1 Fred 0 1
2 George 0 1
This can be done much more elegantly by using a better data structure, like a dict
to map names to sexes:
>>> sex = {'Fred': 'Male', 'George': 'Male', 'Alice': 'Female', 'Emily': 'Female'}
>>> result = pd.concat([df, pd.get_dummies(df['name'].map(sex))], axis=1)
>>> result
name Female Male
0 Alice 1 0
1 Fred 0 1
2 George 0 1
If we have to start from
male = ['fred','george']
female = ['alice','emily']
you can build sex
like this:
>>> sex = {name.capitalize():s for names, s in [(male, 'Male'), (female, 'Female')]
...: for name in names}
...:
>>> sex
{'Alice': 'Female', 'Emily': 'Female', 'Fred': 'Male', 'George': 'Male'}
Finally, if the order of the columns is important, you can reindex the result.
>>> result = result.reindex(columns=['name', 'Male', 'Female'])
>>> result
name Male Female
0 Alice 0 1
1 Fred 1 0
2 George 1 0
1
Thanks for explaining this makes a lot of sense
– fred.schwartz
Nov 22 at 16:35
add a 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%2f53433414%2fusing-a-list-as-a-string-of-the-name-and-the-elements-within-the-list%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
While there is a way, it's not recommended. Just use a dictionary:
d = {'male': ['fred', 'george'],
'female': ['alice', 'emily']}
for k, v in d.items():
mask = df['name'].str.lower().str.contains('|'.join(v), na=False)
df[k.capitalize()] = mask.astype(int)
mask.astype(int)
works because a Boolean array may be mapped directly to 1
/ 0
, just as bool
is a subclass of int
in regular Python.
Result:
print(df)
name Male Female
0 Alice 0 1
1 Fred 1 0
2 George 1 0
1
Thanks alot this worked very nicely
– fred.schwartz
Nov 22 at 16:35
add a comment |
While there is a way, it's not recommended. Just use a dictionary:
d = {'male': ['fred', 'george'],
'female': ['alice', 'emily']}
for k, v in d.items():
mask = df['name'].str.lower().str.contains('|'.join(v), na=False)
df[k.capitalize()] = mask.astype(int)
mask.astype(int)
works because a Boolean array may be mapped directly to 1
/ 0
, just as bool
is a subclass of int
in regular Python.
Result:
print(df)
name Male Female
0 Alice 0 1
1 Fred 1 0
2 George 1 0
1
Thanks alot this worked very nicely
– fred.schwartz
Nov 22 at 16:35
add a comment |
While there is a way, it's not recommended. Just use a dictionary:
d = {'male': ['fred', 'george'],
'female': ['alice', 'emily']}
for k, v in d.items():
mask = df['name'].str.lower().str.contains('|'.join(v), na=False)
df[k.capitalize()] = mask.astype(int)
mask.astype(int)
works because a Boolean array may be mapped directly to 1
/ 0
, just as bool
is a subclass of int
in regular Python.
Result:
print(df)
name Male Female
0 Alice 0 1
1 Fred 1 0
2 George 1 0
While there is a way, it's not recommended. Just use a dictionary:
d = {'male': ['fred', 'george'],
'female': ['alice', 'emily']}
for k, v in d.items():
mask = df['name'].str.lower().str.contains('|'.join(v), na=False)
df[k.capitalize()] = mask.astype(int)
mask.astype(int)
works because a Boolean array may be mapped directly to 1
/ 0
, just as bool
is a subclass of int
in regular Python.
Result:
print(df)
name Male Female
0 Alice 0 1
1 Fred 1 0
2 George 1 0
answered Nov 22 at 14:52
jpp
90.5k2052101
90.5k2052101
1
Thanks alot this worked very nicely
– fred.schwartz
Nov 22 at 16:35
add a comment |
1
Thanks alot this worked very nicely
– fred.schwartz
Nov 22 at 16:35
1
1
Thanks alot this worked very nicely
– fred.schwartz
Nov 22 at 16:35
Thanks alot this worked very nicely
– fred.schwartz
Nov 22 at 16:35
add a comment |
You can use pandas.get_dummies
.
>>> males = {'fred', 'george'}
>>> fm = pd.get_dummies(['Male' if name.lower() in males else 'Female' for name in df['name']])
>>> result = pd.concat([df, fm], axis=1)
>>>
>>> result
name Female Male
0 Alice 1 0
1 Fred 0 1
2 George 0 1
This can be done much more elegantly by using a better data structure, like a dict
to map names to sexes:
>>> sex = {'Fred': 'Male', 'George': 'Male', 'Alice': 'Female', 'Emily': 'Female'}
>>> result = pd.concat([df, pd.get_dummies(df['name'].map(sex))], axis=1)
>>> result
name Female Male
0 Alice 1 0
1 Fred 0 1
2 George 0 1
If we have to start from
male = ['fred','george']
female = ['alice','emily']
you can build sex
like this:
>>> sex = {name.capitalize():s for names, s in [(male, 'Male'), (female, 'Female')]
...: for name in names}
...:
>>> sex
{'Alice': 'Female', 'Emily': 'Female', 'Fred': 'Male', 'George': 'Male'}
Finally, if the order of the columns is important, you can reindex the result.
>>> result = result.reindex(columns=['name', 'Male', 'Female'])
>>> result
name Male Female
0 Alice 0 1
1 Fred 1 0
2 George 1 0
1
Thanks for explaining this makes a lot of sense
– fred.schwartz
Nov 22 at 16:35
add a comment |
You can use pandas.get_dummies
.
>>> males = {'fred', 'george'}
>>> fm = pd.get_dummies(['Male' if name.lower() in males else 'Female' for name in df['name']])
>>> result = pd.concat([df, fm], axis=1)
>>>
>>> result
name Female Male
0 Alice 1 0
1 Fred 0 1
2 George 0 1
This can be done much more elegantly by using a better data structure, like a dict
to map names to sexes:
>>> sex = {'Fred': 'Male', 'George': 'Male', 'Alice': 'Female', 'Emily': 'Female'}
>>> result = pd.concat([df, pd.get_dummies(df['name'].map(sex))], axis=1)
>>> result
name Female Male
0 Alice 1 0
1 Fred 0 1
2 George 0 1
If we have to start from
male = ['fred','george']
female = ['alice','emily']
you can build sex
like this:
>>> sex = {name.capitalize():s for names, s in [(male, 'Male'), (female, 'Female')]
...: for name in names}
...:
>>> sex
{'Alice': 'Female', 'Emily': 'Female', 'Fred': 'Male', 'George': 'Male'}
Finally, if the order of the columns is important, you can reindex the result.
>>> result = result.reindex(columns=['name', 'Male', 'Female'])
>>> result
name Male Female
0 Alice 0 1
1 Fred 1 0
2 George 1 0
1
Thanks for explaining this makes a lot of sense
– fred.schwartz
Nov 22 at 16:35
add a comment |
You can use pandas.get_dummies
.
>>> males = {'fred', 'george'}
>>> fm = pd.get_dummies(['Male' if name.lower() in males else 'Female' for name in df['name']])
>>> result = pd.concat([df, fm], axis=1)
>>>
>>> result
name Female Male
0 Alice 1 0
1 Fred 0 1
2 George 0 1
This can be done much more elegantly by using a better data structure, like a dict
to map names to sexes:
>>> sex = {'Fred': 'Male', 'George': 'Male', 'Alice': 'Female', 'Emily': 'Female'}
>>> result = pd.concat([df, pd.get_dummies(df['name'].map(sex))], axis=1)
>>> result
name Female Male
0 Alice 1 0
1 Fred 0 1
2 George 0 1
If we have to start from
male = ['fred','george']
female = ['alice','emily']
you can build sex
like this:
>>> sex = {name.capitalize():s for names, s in [(male, 'Male'), (female, 'Female')]
...: for name in names}
...:
>>> sex
{'Alice': 'Female', 'Emily': 'Female', 'Fred': 'Male', 'George': 'Male'}
Finally, if the order of the columns is important, you can reindex the result.
>>> result = result.reindex(columns=['name', 'Male', 'Female'])
>>> result
name Male Female
0 Alice 0 1
1 Fred 1 0
2 George 1 0
You can use pandas.get_dummies
.
>>> males = {'fred', 'george'}
>>> fm = pd.get_dummies(['Male' if name.lower() in males else 'Female' for name in df['name']])
>>> result = pd.concat([df, fm], axis=1)
>>>
>>> result
name Female Male
0 Alice 1 0
1 Fred 0 1
2 George 0 1
This can be done much more elegantly by using a better data structure, like a dict
to map names to sexes:
>>> sex = {'Fred': 'Male', 'George': 'Male', 'Alice': 'Female', 'Emily': 'Female'}
>>> result = pd.concat([df, pd.get_dummies(df['name'].map(sex))], axis=1)
>>> result
name Female Male
0 Alice 1 0
1 Fred 0 1
2 George 0 1
If we have to start from
male = ['fred','george']
female = ['alice','emily']
you can build sex
like this:
>>> sex = {name.capitalize():s for names, s in [(male, 'Male'), (female, 'Female')]
...: for name in names}
...:
>>> sex
{'Alice': 'Female', 'Emily': 'Female', 'Fred': 'Male', 'George': 'Male'}
Finally, if the order of the columns is important, you can reindex the result.
>>> result = result.reindex(columns=['name', 'Male', 'Female'])
>>> result
name Male Female
0 Alice 0 1
1 Fred 1 0
2 George 1 0
edited Nov 22 at 15:18
answered Nov 22 at 14:58
timgeb
48.9k116390
48.9k116390
1
Thanks for explaining this makes a lot of sense
– fred.schwartz
Nov 22 at 16:35
add a comment |
1
Thanks for explaining this makes a lot of sense
– fred.schwartz
Nov 22 at 16:35
1
1
Thanks for explaining this makes a lot of sense
– fred.schwartz
Nov 22 at 16:35
Thanks for explaining this makes a lot of sense
– fred.schwartz
Nov 22 at 16:35
add a 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53433414%2fusing-a-list-as-a-string-of-the-name-and-the-elements-within-the-list%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