How to reduce columns in dataframe pandas
Here how the datalooks like in df
dataframe:
A B C D
0.js 2 1 1 -1
1.js 3 -5 1 -4
total 5 -4 2 -5
And I would get new dataframe df1
:
A C
0.js 2 1
1.js 3 1
total 5 2
So basically it should look like this:
df1 = df[df["total"] > 0]
but it should filter on row instead of column and I can't figure it out..
python pandas dataframe
add a comment |
Here how the datalooks like in df
dataframe:
A B C D
0.js 2 1 1 -1
1.js 3 -5 1 -4
total 5 -4 2 -5
And I would get new dataframe df1
:
A C
0.js 2 1
1.js 3 1
total 5 2
So basically it should look like this:
df1 = df[df["total"] > 0]
but it should filter on row instead of column and I can't figure it out..
python pandas dataframe
2
Do you want to only keep the columns where the condition is met ALWAYS, even if the condition is not met once in another column.
– Edeki Okoh
Feb 5 at 20:26
2
Can't you transpose and do the filter?
– Andrew Naguib
Feb 5 at 20:27
YourDataFrame
is just a single row?
– ALollz
Feb 5 at 20:30
not a single row
– J Oderberg
Feb 5 at 20:32
Please, add a reproducible dataframe :)
– Andrew Naguib
Feb 5 at 20:39
add a comment |
Here how the datalooks like in df
dataframe:
A B C D
0.js 2 1 1 -1
1.js 3 -5 1 -4
total 5 -4 2 -5
And I would get new dataframe df1
:
A C
0.js 2 1
1.js 3 1
total 5 2
So basically it should look like this:
df1 = df[df["total"] > 0]
but it should filter on row instead of column and I can't figure it out..
python pandas dataframe
Here how the datalooks like in df
dataframe:
A B C D
0.js 2 1 1 -1
1.js 3 -5 1 -4
total 5 -4 2 -5
And I would get new dataframe df1
:
A C
0.js 2 1
1.js 3 1
total 5 2
So basically it should look like this:
df1 = df[df["total"] > 0]
but it should filter on row instead of column and I can't figure it out..
python pandas dataframe
python pandas dataframe
edited Feb 5 at 20:50
Scott Boston
56.1k73157
56.1k73157
asked Feb 5 at 20:24
J OderbergJ Oderberg
404
404
2
Do you want to only keep the columns where the condition is met ALWAYS, even if the condition is not met once in another column.
– Edeki Okoh
Feb 5 at 20:26
2
Can't you transpose and do the filter?
– Andrew Naguib
Feb 5 at 20:27
YourDataFrame
is just a single row?
– ALollz
Feb 5 at 20:30
not a single row
– J Oderberg
Feb 5 at 20:32
Please, add a reproducible dataframe :)
– Andrew Naguib
Feb 5 at 20:39
add a comment |
2
Do you want to only keep the columns where the condition is met ALWAYS, even if the condition is not met once in another column.
– Edeki Okoh
Feb 5 at 20:26
2
Can't you transpose and do the filter?
– Andrew Naguib
Feb 5 at 20:27
YourDataFrame
is just a single row?
– ALollz
Feb 5 at 20:30
not a single row
– J Oderberg
Feb 5 at 20:32
Please, add a reproducible dataframe :)
– Andrew Naguib
Feb 5 at 20:39
2
2
Do you want to only keep the columns where the condition is met ALWAYS, even if the condition is not met once in another column.
– Edeki Okoh
Feb 5 at 20:26
Do you want to only keep the columns where the condition is met ALWAYS, even if the condition is not met once in another column.
– Edeki Okoh
Feb 5 at 20:26
2
2
Can't you transpose and do the filter?
– Andrew Naguib
Feb 5 at 20:27
Can't you transpose and do the filter?
– Andrew Naguib
Feb 5 at 20:27
Your
DataFrame
is just a single row?– ALollz
Feb 5 at 20:30
Your
DataFrame
is just a single row?– ALollz
Feb 5 at 20:30
not a single row
– J Oderberg
Feb 5 at 20:32
not a single row
– J Oderberg
Feb 5 at 20:32
Please, add a reproducible dataframe :)
– Andrew Naguib
Feb 5 at 20:39
Please, add a reproducible dataframe :)
– Andrew Naguib
Feb 5 at 20:39
add a comment |
3 Answers
3
active
oldest
votes
You can use, loc with boolean indexing or reindex:
df.loc[:, df.columns[(df.loc['total'] > 0)]]
OR
df.reindex(df.columns[(df.loc['total'] > 0)], axis=1)
Output:
A C
0.js 2 1
1.js 3 1
total 5 2
add a comment |
You want to use .loc[:, column_mask]
i.e.
In [11]: df.loc[:, df.sum() > 0]
Out[11]:
A C
total 5 2
# or
In [12]: df.loc[:, df.iloc[0] > 0]
Out[12]:
A C
total 5 2
add a comment |
Use .where
to set negative values to NaN
and then dropna
setting axis = 1
:
df.where(df.gt(0)).dropna(axis=1)
A C
total 5 2
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%2f54542470%2fhow-to-reduce-columns-in-dataframe-pandas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use, loc with boolean indexing or reindex:
df.loc[:, df.columns[(df.loc['total'] > 0)]]
OR
df.reindex(df.columns[(df.loc['total'] > 0)], axis=1)
Output:
A C
0.js 2 1
1.js 3 1
total 5 2
add a comment |
You can use, loc with boolean indexing or reindex:
df.loc[:, df.columns[(df.loc['total'] > 0)]]
OR
df.reindex(df.columns[(df.loc['total'] > 0)], axis=1)
Output:
A C
0.js 2 1
1.js 3 1
total 5 2
add a comment |
You can use, loc with boolean indexing or reindex:
df.loc[:, df.columns[(df.loc['total'] > 0)]]
OR
df.reindex(df.columns[(df.loc['total'] > 0)], axis=1)
Output:
A C
0.js 2 1
1.js 3 1
total 5 2
You can use, loc with boolean indexing or reindex:
df.loc[:, df.columns[(df.loc['total'] > 0)]]
OR
df.reindex(df.columns[(df.loc['total'] > 0)], axis=1)
Output:
A C
0.js 2 1
1.js 3 1
total 5 2
answered Feb 5 at 20:48
Scott BostonScott Boston
56.1k73157
56.1k73157
add a comment |
add a comment |
You want to use .loc[:, column_mask]
i.e.
In [11]: df.loc[:, df.sum() > 0]
Out[11]:
A C
total 5 2
# or
In [12]: df.loc[:, df.iloc[0] > 0]
Out[12]:
A C
total 5 2
add a comment |
You want to use .loc[:, column_mask]
i.e.
In [11]: df.loc[:, df.sum() > 0]
Out[11]:
A C
total 5 2
# or
In [12]: df.loc[:, df.iloc[0] > 0]
Out[12]:
A C
total 5 2
add a comment |
You want to use .loc[:, column_mask]
i.e.
In [11]: df.loc[:, df.sum() > 0]
Out[11]:
A C
total 5 2
# or
In [12]: df.loc[:, df.iloc[0] > 0]
Out[12]:
A C
total 5 2
You want to use .loc[:, column_mask]
i.e.
In [11]: df.loc[:, df.sum() > 0]
Out[11]:
A C
total 5 2
# or
In [12]: df.loc[:, df.iloc[0] > 0]
Out[12]:
A C
total 5 2
answered Feb 5 at 20:27
Andy HaydenAndy Hayden
186k52434422
186k52434422
add a comment |
add a comment |
Use .where
to set negative values to NaN
and then dropna
setting axis = 1
:
df.where(df.gt(0)).dropna(axis=1)
A C
total 5 2
add a comment |
Use .where
to set negative values to NaN
and then dropna
setting axis = 1
:
df.where(df.gt(0)).dropna(axis=1)
A C
total 5 2
add a comment |
Use .where
to set negative values to NaN
and then dropna
setting axis = 1
:
df.where(df.gt(0)).dropna(axis=1)
A C
total 5 2
Use .where
to set negative values to NaN
and then dropna
setting axis = 1
:
df.where(df.gt(0)).dropna(axis=1)
A C
total 5 2
edited Feb 5 at 20:32
answered Feb 5 at 20:27
yatuyatu
12k31339
12k31339
add a comment |
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%2f54542470%2fhow-to-reduce-columns-in-dataframe-pandas%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
2
Do you want to only keep the columns where the condition is met ALWAYS, even if the condition is not met once in another column.
– Edeki Okoh
Feb 5 at 20:26
2
Can't you transpose and do the filter?
– Andrew Naguib
Feb 5 at 20:27
Your
DataFrame
is just a single row?– ALollz
Feb 5 at 20:30
not a single row
– J Oderberg
Feb 5 at 20:32
Please, add a reproducible dataframe :)
– Andrew Naguib
Feb 5 at 20:39