python – 基于pandas中的列中的值从DataFrame中选择行

要选择列值等于标量some​​_value的行,请使用==:

<span class="pln">df<span class="pun">.<span class="pln">loc<span class="pun">[<span class="pln">df<span class="pun">[<span class="str">'column_name'<span class="pun">]<span class="pln"> <span class="pun">==<span class="pln"> some_value<span class="pun">]</span></span></span></span></span></span></span></span></span></span></span></span>

要选择其列值在可迭代值some_values中的行,请使用isin:

<span class="pln">df<span class="pun">.<span class="pln">loc<span class="pun">[<span class="pln">df<span class="pun">[<span class="str">'column_name'<span class="pun">].<span class="pln">isin<span class="pun">(<span class="pln">some_values<span class="pun">)]</span></span></span></span></span></span></span></span></span></span></span></span>

要选择列值不等于some_value的行,请使用!=:

<span class="pln">df<span class="pun">.<span class="pln">loc<span class="pun">[<span class="pln">df<span class="pun">[<span class="str">'column_name'<span class="pun">]<span class="pln"> <span class="pun">!=<span class="pln"> some_value<span class="pun">]</span></span></span></span></span></span></span></span></span></span></span></span>

isin返回一个布尔系列,所以要选择值不在some_values的行,使用〜来否定布尔系列:

<span class="pln">df<span class="pun">.<span class="pln">loc<span class="pun">[~<span class="pln">df<span class="pun">[<span class="str">'column_name'<span class="pun">].<span class="pln">isin<span class="pun">(<span class="pln">some_values<span class="pun">)]</span></span></span></span></span></span></span></span></span></span></span></span>

例如,

<span class="kwd">import<span class="pln"> pandas <span class="kwd">as<span class="pln"> pd
<span class="kwd">import<span class="pln"> numpy <span class="kwd">as<span class="pln"> np
df <span class="pun">=<span class="pln"> pd<span class="pun">.<span class="typ">DataFrame<span class="pun">({<span class="str">'A'<span class="pun">:<span class="pln"> <span class="str">'foo bar foo bar foo bar foo foo'<span class="pun">.<span class="pln">split<span class="pun">(),<span class="pln">
                   <span class="str">'B'<span class="pun">:<span class="pln"> <span class="str">'one one two three two two one three'<span class="pun">.<span class="pln">split<span class="pun">(),<span class="pln">
                   <span class="str">'C'<span class="pun">:<span class="pln"> np<span class="pun">.<span class="pln">arange<span class="pun">(<span class="lit">8<span class="pun">),<span class="pln"> <span class="str">'D'<span class="pun">:<span class="pln"> np<span class="pun">.<span class="pln">arange<span class="pun">(<span class="lit">8<span class="pun">)<span class="pln"> <span class="pun">*<span class="pln"> <span class="lit">2<span class="pun">})<span class="pln">
<span class="kwd">print<span class="pun">(<span class="pln">df<span class="pun">)<span class="pln">
<span class="com">#      A      B  C   D<span class="pln">
<span class="com"># 0  foo    one  0   0<span class="pln">
<span class="com"># 1  bar    one  1   2<span class="pln">
<span class="com"># 2  foo    two  2   4<span class="pln">
<span class="com"># 3  bar  three  3   6<span class="pln">
<span class="com"># 4  foo    two  4   8<span class="pln">
<span class="com"># 5  bar    two  5  10<span class="pln">
<span class="com"># 6  foo    one  6  12<span class="pln">
<span class="com"># 7  foo  three  7  14<span class="pln">

<span class="kwd">print<span class="pun">(<span class="pln">df<span class="pun">.<span class="pln">loc<span class="pun">[<span class="pln">df<span class="pun">[<span class="str">'A'<span class="pun">]<span class="pln"> <span class="pun">==<span class="pln"> <span class="str">'foo'<span class="pun">])</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="pln">     A      B  C   D
<span class="lit">0<span class="pln">  foo    one  <span class="lit">0<span class="pln">   <span class="lit">0<span class="pln">
<span class="lit">2<span class="pln">  foo    two  <span class="lit">2<span class="pln">   <span class="lit">4<span class="pln">
<span class="lit">4<span class="pln">  foo    two  <span class="lit">4<span class="pln">   <span class="lit">8<span class="pln">
<span class="lit">6<span class="pln">  foo    one  <span class="lit">6<span class="pln">  <span class="lit">12<span class="pln">
<span class="lit">7<span class="pln">  foo  three  <span class="lit">7<span class="pln">  <span class="lit">14</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

如果您想要包含多个值,请将它们放入
列表(或更一般地,任何可迭代),并使用isin:

<span class="kwd">print<span class="pun">(<span class="pln">df<span class="pun">.<span class="pln">loc<span class="pun">[<span class="pln">df<span class="pun">[<span class="str">'B'<span class="pun">].<span class="pln">isin<span class="pun">([<span class="str">'one'<span class="pun">,<span class="str">'three'<span class="pun">])])</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="pln">     A      B  C   D
<span class="lit">0<span class="pln">  foo    one  <span class="lit">0<span class="pln">   <span class="lit">0<span class="pln">
<span class="lit">1<span class="pln">  bar    one  <span class="lit">1<span class="pln">   <span class="lit">2<span class="pln">
<span class="lit">3<span class="pln">  bar  three  <span class="lit">3<span class="pln">   <span class="lit">6<span class="pln">
<span class="lit">6<span class="pln">  foo    one  <span class="lit">6<span class="pln">  <span class="lit">12<span class="pln">
<span class="lit">7<span class="pln">  foo  three  <span class="lit">7<span class="pln">  <span class="lit">14</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

但请注意,如果你想这样做很多次,它是更有效的
首先创建一个索引,然后使用df.loc:

<span class="pln">df <span class="pun">=<span class="pln"> df<span class="pun">.<span class="pln">set_index<span class="pun">([<span class="str">'B'<span class="pun">])<span class="pln">
<span class="kwd">print<span class="pun">(<span class="pln">df<span class="pun">.<span class="pln">loc<span class="pun">[<span class="str">'one'<span class="pun">])</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="pln">       A  C   D
B
one  foo  <span class="lit">0<span class="pln">   <span class="lit">0<span class="pln">
one  bar  <span class="lit">1<span class="pln">   <span class="lit">2<span class="pln">
one  foo  <span class="lit">6<span class="pln">  <span class="lit">12</span></span></span></span></span></span></span></span></span></span></span></span>

或者,从索引中包含多个值使用df.index.isin:

<span class="pln">df<span class="pun">.<span class="pln">loc<span class="pun">[<span class="pln">df<span class="pun">.<span class="pln">index<span class="pun">.<span class="pln">isin<span class="pun">([<span class="str">'one'<span class="pun">,<span class="str">'two'<span class="pun">])]</span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="pln">       A  C   D
B
one  foo  <span class="lit">0<span class="pln">   <span class="lit">0<span class="pln">
one  bar  <span class="lit">1<span class="pln">   <span class="lit">2<span class="pln">
two  foo  <span class="lit">2<span class="pln">   <span class="lit">4<span class="pln">
two  foo  <span class="lit">4<span class="pln">   <span class="lit">8<span class="pln">
two  bar  <span class="lit">5<span class="pln">  <span class="lit">10<span class="pln">
one  foo  <span class="lit">6<span class="pln">  <span class="lit">12</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

Original: https://www.cnblogs.com/to-creat/p/7724562.html
Author: Wei_java
Title: python – 基于pandas中的列中的值从DataFrame中选择行

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/9138/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总