Comparing column values within a row within a Pandas Dataframe
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a specific and a general problem I'm trying to solve.
Specific Problem:
I want to create a new column in a data frame that gives a 1 if column C1 is 8 and all other values in the row are less than 8.How do I logically negate all of the other columns at the same time? Here is the code from my flawed attempt:
df["C1is_8"] = df.apply(lambda row:(row['C1']==8)& ~(row['C1']<8) ,axis=1).astype(int)
The code below produces the dataframe for the code above.
dict = { 'C1':[4,3,0,0,2,3,4,5,8,8,8,8],
'C2':[8,3,3,7,6,5,3,5,6,8,8,8],
'C3':[2,3,6,4,5,0,0,4,6,7,8,8],
'C4':[8,5,4,4,4,3,2,1,4,2,6,8]
}
columns = ['C1','C2','C3','C4']
Index = [1,2,3,4,5,6,7,8,9,10,11,12]
df = pd.DataFrame(dict,index = Index,columns = columns)
df = OGdf[::-1]
df
General Problem: How do I rewrite some version of the code above so that I can generalize it (i.e. row[ i ] ) so that it could apply to any column not just 'C1'?
python pandas
add a comment |
I have a specific and a general problem I'm trying to solve.
Specific Problem:
I want to create a new column in a data frame that gives a 1 if column C1 is 8 and all other values in the row are less than 8.How do I logically negate all of the other columns at the same time? Here is the code from my flawed attempt:
df["C1is_8"] = df.apply(lambda row:(row['C1']==8)& ~(row['C1']<8) ,axis=1).astype(int)
The code below produces the dataframe for the code above.
dict = { 'C1':[4,3,0,0,2,3,4,5,8,8,8,8],
'C2':[8,3,3,7,6,5,3,5,6,8,8,8],
'C3':[2,3,6,4,5,0,0,4,6,7,8,8],
'C4':[8,5,4,4,4,3,2,1,4,2,6,8]
}
columns = ['C1','C2','C3','C4']
Index = [1,2,3,4,5,6,7,8,9,10,11,12]
df = pd.DataFrame(dict,index = Index,columns = columns)
df = OGdf[::-1]
df
General Problem: How do I rewrite some version of the code above so that I can generalize it (i.e. row[ i ] ) so that it could apply to any column not just 'C1'?
python pandas
add a comment |
I have a specific and a general problem I'm trying to solve.
Specific Problem:
I want to create a new column in a data frame that gives a 1 if column C1 is 8 and all other values in the row are less than 8.How do I logically negate all of the other columns at the same time? Here is the code from my flawed attempt:
df["C1is_8"] = df.apply(lambda row:(row['C1']==8)& ~(row['C1']<8) ,axis=1).astype(int)
The code below produces the dataframe for the code above.
dict = { 'C1':[4,3,0,0,2,3,4,5,8,8,8,8],
'C2':[8,3,3,7,6,5,3,5,6,8,8,8],
'C3':[2,3,6,4,5,0,0,4,6,7,8,8],
'C4':[8,5,4,4,4,3,2,1,4,2,6,8]
}
columns = ['C1','C2','C3','C4']
Index = [1,2,3,4,5,6,7,8,9,10,11,12]
df = pd.DataFrame(dict,index = Index,columns = columns)
df = OGdf[::-1]
df
General Problem: How do I rewrite some version of the code above so that I can generalize it (i.e. row[ i ] ) so that it could apply to any column not just 'C1'?
python pandas
I have a specific and a general problem I'm trying to solve.
Specific Problem:
I want to create a new column in a data frame that gives a 1 if column C1 is 8 and all other values in the row are less than 8.How do I logically negate all of the other columns at the same time? Here is the code from my flawed attempt:
df["C1is_8"] = df.apply(lambda row:(row['C1']==8)& ~(row['C1']<8) ,axis=1).astype(int)
The code below produces the dataframe for the code above.
dict = { 'C1':[4,3,0,0,2,3,4,5,8,8,8,8],
'C2':[8,3,3,7,6,5,3,5,6,8,8,8],
'C3':[2,3,6,4,5,0,0,4,6,7,8,8],
'C4':[8,5,4,4,4,3,2,1,4,2,6,8]
}
columns = ['C1','C2','C3','C4']
Index = [1,2,3,4,5,6,7,8,9,10,11,12]
df = pd.DataFrame(dict,index = Index,columns = columns)
df = OGdf[::-1]
df
General Problem: How do I rewrite some version of the code above so that I can generalize it (i.e. row[ i ] ) so that it could apply to any column not just 'C1'?
python pandas
python pandas
asked Nov 17 '18 at 0:48
rer49rer49
1017
1017
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I think this answer satisfies both your specific and your general problems, using filter and all:
# define the column you want to apply your first condition to
col = 'C1'
# Python 3.6 or above, with f-strings:
df['new_col'] = ((df[col] == 8) & (df.filter(regex=f'[^{col}]') < 8).all(1)).astype(int)
# Otherwise:
df['new_col'] = ((df[col] == 8) & (df.filter(regex='[^{}]'.format(col)) < 8).all(1)).astype(int)
>>> df
C1 C2 C3 C4 new_col
1 4 8 2 8 0
2 3 3 3 5 0
3 0 3 6 4 0
4 0 7 4 4 0
5 2 6 5 4 0
6 3 5 0 3 0
7 4 3 0 2 0
8 5 5 4 1 0
9 8 6 6 4 1
10 8 8 7 2 0
11 8 8 8 6 0
12 8 8 8 8 0
@ sacul, will mind explainingall, though i understandpandas.Series.all : Return True if all elements are True, i think when you sayall(1)ie across the columns
– pygo
Nov 20 '18 at 4:35
@pygo, that's right, it's checking that all values (besides the one filtered out) in a row (i.e. across all columns) satisfies the< 8condition
– sacuL
Nov 20 '18 at 4:48
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%2f53347175%2fcomparing-column-values-within-a-row-within-a-pandas-dataframe%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
I think this answer satisfies both your specific and your general problems, using filter and all:
# define the column you want to apply your first condition to
col = 'C1'
# Python 3.6 or above, with f-strings:
df['new_col'] = ((df[col] == 8) & (df.filter(regex=f'[^{col}]') < 8).all(1)).astype(int)
# Otherwise:
df['new_col'] = ((df[col] == 8) & (df.filter(regex='[^{}]'.format(col)) < 8).all(1)).astype(int)
>>> df
C1 C2 C3 C4 new_col
1 4 8 2 8 0
2 3 3 3 5 0
3 0 3 6 4 0
4 0 7 4 4 0
5 2 6 5 4 0
6 3 5 0 3 0
7 4 3 0 2 0
8 5 5 4 1 0
9 8 6 6 4 1
10 8 8 7 2 0
11 8 8 8 6 0
12 8 8 8 8 0
@ sacul, will mind explainingall, though i understandpandas.Series.all : Return True if all elements are True, i think when you sayall(1)ie across the columns
– pygo
Nov 20 '18 at 4:35
@pygo, that's right, it's checking that all values (besides the one filtered out) in a row (i.e. across all columns) satisfies the< 8condition
– sacuL
Nov 20 '18 at 4:48
add a comment |
I think this answer satisfies both your specific and your general problems, using filter and all:
# define the column you want to apply your first condition to
col = 'C1'
# Python 3.6 or above, with f-strings:
df['new_col'] = ((df[col] == 8) & (df.filter(regex=f'[^{col}]') < 8).all(1)).astype(int)
# Otherwise:
df['new_col'] = ((df[col] == 8) & (df.filter(regex='[^{}]'.format(col)) < 8).all(1)).astype(int)
>>> df
C1 C2 C3 C4 new_col
1 4 8 2 8 0
2 3 3 3 5 0
3 0 3 6 4 0
4 0 7 4 4 0
5 2 6 5 4 0
6 3 5 0 3 0
7 4 3 0 2 0
8 5 5 4 1 0
9 8 6 6 4 1
10 8 8 7 2 0
11 8 8 8 6 0
12 8 8 8 8 0
@ sacul, will mind explainingall, though i understandpandas.Series.all : Return True if all elements are True, i think when you sayall(1)ie across the columns
– pygo
Nov 20 '18 at 4:35
@pygo, that's right, it's checking that all values (besides the one filtered out) in a row (i.e. across all columns) satisfies the< 8condition
– sacuL
Nov 20 '18 at 4:48
add a comment |
I think this answer satisfies both your specific and your general problems, using filter and all:
# define the column you want to apply your first condition to
col = 'C1'
# Python 3.6 or above, with f-strings:
df['new_col'] = ((df[col] == 8) & (df.filter(regex=f'[^{col}]') < 8).all(1)).astype(int)
# Otherwise:
df['new_col'] = ((df[col] == 8) & (df.filter(regex='[^{}]'.format(col)) < 8).all(1)).astype(int)
>>> df
C1 C2 C3 C4 new_col
1 4 8 2 8 0
2 3 3 3 5 0
3 0 3 6 4 0
4 0 7 4 4 0
5 2 6 5 4 0
6 3 5 0 3 0
7 4 3 0 2 0
8 5 5 4 1 0
9 8 6 6 4 1
10 8 8 7 2 0
11 8 8 8 6 0
12 8 8 8 8 0
I think this answer satisfies both your specific and your general problems, using filter and all:
# define the column you want to apply your first condition to
col = 'C1'
# Python 3.6 or above, with f-strings:
df['new_col'] = ((df[col] == 8) & (df.filter(regex=f'[^{col}]') < 8).all(1)).astype(int)
# Otherwise:
df['new_col'] = ((df[col] == 8) & (df.filter(regex='[^{}]'.format(col)) < 8).all(1)).astype(int)
>>> df
C1 C2 C3 C4 new_col
1 4 8 2 8 0
2 3 3 3 5 0
3 0 3 6 4 0
4 0 7 4 4 0
5 2 6 5 4 0
6 3 5 0 3 0
7 4 3 0 2 0
8 5 5 4 1 0
9 8 6 6 4 1
10 8 8 7 2 0
11 8 8 8 6 0
12 8 8 8 8 0
answered Nov 17 '18 at 0:58
sacuLsacuL
30.9k42144
30.9k42144
@ sacul, will mind explainingall, though i understandpandas.Series.all : Return True if all elements are True, i think when you sayall(1)ie across the columns
– pygo
Nov 20 '18 at 4:35
@pygo, that's right, it's checking that all values (besides the one filtered out) in a row (i.e. across all columns) satisfies the< 8condition
– sacuL
Nov 20 '18 at 4:48
add a comment |
@ sacul, will mind explainingall, though i understandpandas.Series.all : Return True if all elements are True, i think when you sayall(1)ie across the columns
– pygo
Nov 20 '18 at 4:35
@pygo, that's right, it's checking that all values (besides the one filtered out) in a row (i.e. across all columns) satisfies the< 8condition
– sacuL
Nov 20 '18 at 4:48
@ sacul, will mind explaining
all, though i understand pandas.Series.all : Return True if all elements are True , i think when you say all(1) ie across the columns– pygo
Nov 20 '18 at 4:35
@ sacul, will mind explaining
all, though i understand pandas.Series.all : Return True if all elements are True , i think when you say all(1) ie across the columns– pygo
Nov 20 '18 at 4:35
@pygo, that's right, it's checking that all values (besides the one filtered out) in a row (i.e. across all columns) satisfies the
< 8 condition– sacuL
Nov 20 '18 at 4:48
@pygo, that's right, it's checking that all values (besides the one filtered out) in a row (i.e. across all columns) satisfies the
< 8 condition– sacuL
Nov 20 '18 at 4:48
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.
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%2f53347175%2fcomparing-column-values-within-a-row-within-a-pandas-dataframe%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