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.










share|improve this question
























  • 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















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.










share|improve this question
























  • 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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 20:55

























asked Nov 10 at 20:47









user10089194

386




386












  • 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


















  • 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
















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












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.






share|improve this answer





















  • 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




    @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 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








  • 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











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',
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
});


}
});














 

draft saved


draft discarded


















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

























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.






share|improve this answer





















  • 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




    @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 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








  • 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















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.






share|improve this answer





















  • 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




    @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 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








  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 21:12









jezrael

308k20244319




308k20244319












  • 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




    @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 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








  • 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








  • 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 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








  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma