Selecting rows for panda dataframe based on a condition
up vote
1
down vote
favorite
import numpy as np
import pandas as pd
lst2 = [[0.23,"f1"],[5.36,'f2']]
lst2_df = pd.DataFrame(lst2,index=list('pd'),columns=list('ab'))
lst2_df = lst2_df.rename({'a':'A'},axis='columns')
print(lst2_df)
m = ['1','f2']
print(lst2_df.loc[lst2_df['b'].isin(m)])
If I wish to iterate this condition for every column and not hard code it what do I write?
I tried print(lst2_df.loc[lst2_df['A':'b'].isin(m)])
it didnt worked.
I know there are similar question on the site but I could not find one that addresses my issue.
pandas dataframe pycharm
|
show 1 more comment
up vote
1
down vote
favorite
import numpy as np
import pandas as pd
lst2 = [[0.23,"f1"],[5.36,'f2']]
lst2_df = pd.DataFrame(lst2,index=list('pd'),columns=list('ab'))
lst2_df = lst2_df.rename({'a':'A'},axis='columns')
print(lst2_df)
m = ['1','f2']
print(lst2_df.loc[lst2_df['b'].isin(m)])
If I wish to iterate this condition for every column and not hard code it what do I write?
I tried print(lst2_df.loc[lst2_df['A':'b'].isin(m)])
it didnt worked.
I know there are similar question on the site but I could not find one that addresses my issue.
pandas dataframe pycharm
Sorry, what is expected output? Do you needprint([lst2_df.loc[lst2_df[c].isin(m)] for c in lst2_df.columns])
?
– jezrael
Nov 10 at 20:50
I need that it performs the operation for every column. I have hardcoded it for a column 'b'. If I need to do it for every column,what is the solution? I hope i cleared your doubt
– user10089194
Nov 10 at 20:54
Can you explain more? What is expected output?
– jezrael
Nov 10 at 20:56
print(lst2_df.loc[lst2_df['b'].isin(m)])
is hardcoded for specific column 'b'. Let's say I had a huge dataframe and I wish to print rows whose element matches the condition.print(lst2_df.loc[lst2_df['all column'].isin(m)])
– user10089194
Nov 10 at 21:02
1
Something likeprint(lst2_df.loc[lst2_df.isin(m).any(axis=1)])
?
– jezrael
Nov 10 at 21:07
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
import numpy as np
import pandas as pd
lst2 = [[0.23,"f1"],[5.36,'f2']]
lst2_df = pd.DataFrame(lst2,index=list('pd'),columns=list('ab'))
lst2_df = lst2_df.rename({'a':'A'},axis='columns')
print(lst2_df)
m = ['1','f2']
print(lst2_df.loc[lst2_df['b'].isin(m)])
If I wish to iterate this condition for every column and not hard code it what do I write?
I tried print(lst2_df.loc[lst2_df['A':'b'].isin(m)])
it didnt worked.
I know there are similar question on the site but I could not find one that addresses my issue.
pandas dataframe pycharm
import numpy as np
import pandas as pd
lst2 = [[0.23,"f1"],[5.36,'f2']]
lst2_df = pd.DataFrame(lst2,index=list('pd'),columns=list('ab'))
lst2_df = lst2_df.rename({'a':'A'},axis='columns')
print(lst2_df)
m = ['1','f2']
print(lst2_df.loc[lst2_df['b'].isin(m)])
If I wish to iterate this condition for every column and not hard code it what do I write?
I tried print(lst2_df.loc[lst2_df['A':'b'].isin(m)])
it didnt worked.
I know there are similar question on the site but I could not find one that addresses my issue.
pandas dataframe pycharm
pandas dataframe pycharm
edited Nov 10 at 20:55
asked Nov 10 at 20:47
user10089194
386
386
Sorry, what is expected output? Do you needprint([lst2_df.loc[lst2_df[c].isin(m)] for c in lst2_df.columns])
?
– jezrael
Nov 10 at 20:50
I need that it performs the operation for every column. I have hardcoded it for a column 'b'. If I need to do it for every column,what is the solution? I hope i cleared your doubt
– user10089194
Nov 10 at 20:54
Can you explain more? What is expected output?
– jezrael
Nov 10 at 20:56
print(lst2_df.loc[lst2_df['b'].isin(m)])
is hardcoded for specific column 'b'. Let's say I had a huge dataframe and I wish to print rows whose element matches the condition.print(lst2_df.loc[lst2_df['all column'].isin(m)])
– user10089194
Nov 10 at 21:02
1
Something likeprint(lst2_df.loc[lst2_df.isin(m).any(axis=1)])
?
– jezrael
Nov 10 at 21:07
|
show 1 more comment
Sorry, what is expected output? Do you needprint([lst2_df.loc[lst2_df[c].isin(m)] for c in lst2_df.columns])
?
– jezrael
Nov 10 at 20:50
I need that it performs the operation for every column. I have hardcoded it for a column 'b'. If I need to do it for every column,what is the solution? I hope i cleared your doubt
– user10089194
Nov 10 at 20:54
Can you explain more? What is expected output?
– jezrael
Nov 10 at 20:56
print(lst2_df.loc[lst2_df['b'].isin(m)])
is hardcoded for specific column 'b'. Let's say I had a huge dataframe and I wish to print rows whose element matches the condition.print(lst2_df.loc[lst2_df['all column'].isin(m)])
– user10089194
Nov 10 at 21:02
1
Something likeprint(lst2_df.loc[lst2_df.isin(m).any(axis=1)])
?
– jezrael
Nov 10 at 21:07
Sorry, what is expected output? Do you need
print([lst2_df.loc[lst2_df[c].isin(m)] for c in lst2_df.columns])
?– jezrael
Nov 10 at 20:50
Sorry, what is expected output? Do you need
print([lst2_df.loc[lst2_df[c].isin(m)] for c in lst2_df.columns])
?– jezrael
Nov 10 at 20:50
I need that it performs the operation for every column. I have hardcoded it for a column 'b'. If I need to do it for every column,what is the solution? I hope i cleared your doubt
– user10089194
Nov 10 at 20:54
I need that it performs the operation for every column. I have hardcoded it for a column 'b'. If I need to do it for every column,what is the solution? I hope i cleared your doubt
– user10089194
Nov 10 at 20:54
Can you explain more? What is expected output?
– jezrael
Nov 10 at 20:56
Can you explain more? What is expected output?
– jezrael
Nov 10 at 20:56
print(lst2_df.loc[lst2_df['b'].isin(m)])
is hardcoded for specific column 'b'. Let's say I had a huge dataframe and I wish to print rows whose element matches the condition. print(lst2_df.loc[lst2_df['all column'].isin(m)])
– user10089194
Nov 10 at 21:02
print(lst2_df.loc[lst2_df['b'].isin(m)])
is hardcoded for specific column 'b'. Let's say I had a huge dataframe and I wish to print rows whose element matches the condition. print(lst2_df.loc[lst2_df['all column'].isin(m)])
– user10089194
Nov 10 at 21:02
1
1
Something like
print(lst2_df.loc[lst2_df.isin(m).any(axis=1)])
?– jezrael
Nov 10 at 21:07
Something like
print(lst2_df.loc[lst2_df.isin(m).any(axis=1)])
?– jezrael
Nov 10 at 21:07
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Use:
m = ['1','f2']
print(lst2_df.loc[lst2_df.isin(m).any(axis=1)])
A b
d 5.36 f2
Explanation:
Compare DataFrame
(all columns) by DataFrame.isin
for boolean:
print (lst2_df.isin(m))
A b
p False False
d False True
And then add DataFrame.any
for check at least one True per row:
print (lst2_df.isin(m).any(axis=1))
p False
d True
dtype: bool
And last filter by boolean indexing
.
just one doubt what difference will it make if I useprint(lst2_df.loc[lst2_df.isin(m).all(axis=1)])
instead?
– user10089194
Nov 11 at 8:49
1
@user10089194 - It check if all Trues per rows, so filtering only rows with matched all values of m per rows.
– jezrael
Nov 11 at 8:51
1
@user10089194 - if use(lst2_df.isin(m)
get boolean mask. So there is 2 possibility - check if any of value per row is True.any(axis=1)
or check if all values per row isTrue
by.all(axis=1)
. So if not all values matched then.all(axis=1)
return False and output is empty df.
– jezrael
Nov 11 at 8:55
@user10089194 - try test it bym = ['1','f2', 5.36]
with sample data
– jezrael
Nov 11 at 8:57
1
I did I understood..any
is like or. If any of the element matches it will print the entire rows however.all
is and. All the element should match for it to print the row. That is why in my case it gave Empty dataframe as output. Thanks :)
– user10089194
Nov 11 at 9:03
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Use:
m = ['1','f2']
print(lst2_df.loc[lst2_df.isin(m).any(axis=1)])
A b
d 5.36 f2
Explanation:
Compare DataFrame
(all columns) by DataFrame.isin
for boolean:
print (lst2_df.isin(m))
A b
p False False
d False True
And then add DataFrame.any
for check at least one True per row:
print (lst2_df.isin(m).any(axis=1))
p False
d True
dtype: bool
And last filter by boolean indexing
.
just one doubt what difference will it make if I useprint(lst2_df.loc[lst2_df.isin(m).all(axis=1)])
instead?
– user10089194
Nov 11 at 8:49
1
@user10089194 - It check if all Trues per rows, so filtering only rows with matched all values of m per rows.
– jezrael
Nov 11 at 8:51
1
@user10089194 - if use(lst2_df.isin(m)
get boolean mask. So there is 2 possibility - check if any of value per row is True.any(axis=1)
or check if all values per row isTrue
by.all(axis=1)
. So if not all values matched then.all(axis=1)
return False and output is empty df.
– jezrael
Nov 11 at 8:55
@user10089194 - try test it bym = ['1','f2', 5.36]
with sample data
– jezrael
Nov 11 at 8:57
1
I did I understood..any
is like or. If any of the element matches it will print the entire rows however.all
is and. All the element should match for it to print the row. That is why in my case it gave Empty dataframe as output. Thanks :)
– user10089194
Nov 11 at 9:03
|
show 2 more comments
up vote
1
down vote
accepted
Use:
m = ['1','f2']
print(lst2_df.loc[lst2_df.isin(m).any(axis=1)])
A b
d 5.36 f2
Explanation:
Compare DataFrame
(all columns) by DataFrame.isin
for boolean:
print (lst2_df.isin(m))
A b
p False False
d False True
And then add DataFrame.any
for check at least one True per row:
print (lst2_df.isin(m).any(axis=1))
p False
d True
dtype: bool
And last filter by boolean indexing
.
just one doubt what difference will it make if I useprint(lst2_df.loc[lst2_df.isin(m).all(axis=1)])
instead?
– user10089194
Nov 11 at 8:49
1
@user10089194 - It check if all Trues per rows, so filtering only rows with matched all values of m per rows.
– jezrael
Nov 11 at 8:51
1
@user10089194 - if use(lst2_df.isin(m)
get boolean mask. So there is 2 possibility - check if any of value per row is True.any(axis=1)
or check if all values per row isTrue
by.all(axis=1)
. So if not all values matched then.all(axis=1)
return False and output is empty df.
– jezrael
Nov 11 at 8:55
@user10089194 - try test it bym = ['1','f2', 5.36]
with sample data
– jezrael
Nov 11 at 8:57
1
I did I understood..any
is like or. If any of the element matches it will print the entire rows however.all
is and. All the element should match for it to print the row. That is why in my case it gave Empty dataframe as output. Thanks :)
– user10089194
Nov 11 at 9:03
|
show 2 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Use:
m = ['1','f2']
print(lst2_df.loc[lst2_df.isin(m).any(axis=1)])
A b
d 5.36 f2
Explanation:
Compare DataFrame
(all columns) by DataFrame.isin
for boolean:
print (lst2_df.isin(m))
A b
p False False
d False True
And then add DataFrame.any
for check at least one True per row:
print (lst2_df.isin(m).any(axis=1))
p False
d True
dtype: bool
And last filter by boolean indexing
.
Use:
m = ['1','f2']
print(lst2_df.loc[lst2_df.isin(m).any(axis=1)])
A b
d 5.36 f2
Explanation:
Compare DataFrame
(all columns) by DataFrame.isin
for boolean:
print (lst2_df.isin(m))
A b
p False False
d False True
And then add DataFrame.any
for check at least one True per row:
print (lst2_df.isin(m).any(axis=1))
p False
d True
dtype: bool
And last filter by boolean indexing
.
answered Nov 10 at 21:12
jezrael
308k20244319
308k20244319
just one doubt what difference will it make if I useprint(lst2_df.loc[lst2_df.isin(m).all(axis=1)])
instead?
– user10089194
Nov 11 at 8:49
1
@user10089194 - It check if all Trues per rows, so filtering only rows with matched all values of m per rows.
– jezrael
Nov 11 at 8:51
1
@user10089194 - if use(lst2_df.isin(m)
get boolean mask. So there is 2 possibility - check if any of value per row is True.any(axis=1)
or check if all values per row isTrue
by.all(axis=1)
. So if not all values matched then.all(axis=1)
return False and output is empty df.
– jezrael
Nov 11 at 8:55
@user10089194 - try test it bym = ['1','f2', 5.36]
with sample data
– jezrael
Nov 11 at 8:57
1
I did I understood..any
is like or. If any of the element matches it will print the entire rows however.all
is and. All the element should match for it to print the row. That is why in my case it gave Empty dataframe as output. Thanks :)
– user10089194
Nov 11 at 9:03
|
show 2 more comments
just one doubt what difference will it make if I useprint(lst2_df.loc[lst2_df.isin(m).all(axis=1)])
instead?
– user10089194
Nov 11 at 8:49
1
@user10089194 - It check if all Trues per rows, so filtering only rows with matched all values of m per rows.
– jezrael
Nov 11 at 8:51
1
@user10089194 - if use(lst2_df.isin(m)
get boolean mask. So there is 2 possibility - check if any of value per row is True.any(axis=1)
or check if all values per row isTrue
by.all(axis=1)
. So if not all values matched then.all(axis=1)
return False and output is empty df.
– jezrael
Nov 11 at 8:55
@user10089194 - try test it bym = ['1','f2', 5.36]
with sample data
– jezrael
Nov 11 at 8:57
1
I did I understood..any
is like or. If any of the element matches it will print the entire rows however.all
is and. All the element should match for it to print the row. That is why in my case it gave Empty dataframe as output. Thanks :)
– user10089194
Nov 11 at 9:03
just one doubt what difference will it make if I use
print(lst2_df.loc[lst2_df.isin(m).all(axis=1)])
instead?– user10089194
Nov 11 at 8:49
just one doubt what difference will it make if I use
print(lst2_df.loc[lst2_df.isin(m).all(axis=1)])
instead?– user10089194
Nov 11 at 8:49
1
1
@user10089194 - It check if all Trues per rows, so filtering only rows with matched all values of m per rows.
– jezrael
Nov 11 at 8:51
@user10089194 - It check if all Trues per rows, so filtering only rows with matched all values of m per rows.
– jezrael
Nov 11 at 8:51
1
1
@user10089194 - if use
(lst2_df.isin(m)
get boolean mask. So there is 2 possibility - check if any of value per row is True .any(axis=1)
or check if all values per row is True
by .all(axis=1)
. So if not all values matched then .all(axis=1)
return False and output is empty df.– jezrael
Nov 11 at 8:55
@user10089194 - if use
(lst2_df.isin(m)
get boolean mask. So there is 2 possibility - check if any of value per row is True .any(axis=1)
or check if all values per row is True
by .all(axis=1)
. So if not all values matched then .all(axis=1)
return False and output is empty df.– jezrael
Nov 11 at 8:55
@user10089194 - try test it by
m = ['1','f2', 5.36]
with sample data– jezrael
Nov 11 at 8:57
@user10089194 - try test it by
m = ['1','f2', 5.36]
with sample data– jezrael
Nov 11 at 8:57
1
1
I did I understood.
.any
is like or. If any of the element matches it will print the entire rows however .all
is and. All the element should match for it to print the row. That is why in my case it gave Empty dataframe as output. Thanks :)– user10089194
Nov 11 at 9:03
I did I understood.
.any
is like or. If any of the element matches it will print the entire rows however .all
is and. All the element should match for it to print the row. That is why in my case it gave Empty dataframe as output. Thanks :)– user10089194
Nov 11 at 9:03
|
show 2 more comments
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%2f53243248%2fselecting-rows-for-panda-dataframe-based-on-a-condition%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
Sorry, what is expected output? Do you need
print([lst2_df.loc[lst2_df[c].isin(m)] for c in lst2_df.columns])
?– jezrael
Nov 10 at 20:50
I need that it performs the operation for every column. I have hardcoded it for a column 'b'. If I need to do it for every column,what is the solution? I hope i cleared your doubt
– user10089194
Nov 10 at 20:54
Can you explain more? What is expected output?
– jezrael
Nov 10 at 20:56
print(lst2_df.loc[lst2_df['b'].isin(m)])
is hardcoded for specific column 'b'. Let's say I had a huge dataframe and I wish to print rows whose element matches the condition.print(lst2_df.loc[lst2_df['all column'].isin(m)])
– user10089194
Nov 10 at 21:02
1
Something like
print(lst2_df.loc[lst2_df.isin(m).any(axis=1)])
?– jezrael
Nov 10 at 21:07