pandas在不知道列数的情况下选择行

时间:2017-02-06 12:25:39

标签: python pandas

我有一个导入的数据框,我不知道列的数量或列的名称(因为这会有所不同)

在这种情况下,我有一个包含3列的数据框:

a = {'Attempts': [10, 15, 5, 25, 30], '2nd Attempts': [10, 12, 15, 14, 0],
 '3rd Attempts': [10, 10, 9, 11, 10]}
a = pd.DataFrame(a)
print (a)

我如何仅选择仅包含数字10的行。因此,在这种情况下,我将返回第1,2和5行(或0,1和4)。

1 个答案:

答案 0 :(得分:1)

您可以将boolean indexingmask一起使用,将DataFrame中的所有值与10进行比较,然后添加any,以便至少返回一个True print (a == 10) 2nd Attempts 3rd Attempts Attempts 0 True True True 1 False True False 2 False False False 3 False False False 4 False True False mask = (a == 10).any(axis=1) print (mask) 0 True 1 True 2 False 3 False 4 True dtype: bool print (a[mask]) 2nd Attempts 3rd Attempts Attempts 0 10 10 10 1 12 10 15 4 0 10 30 }}:

a = {'Attempts': ['a', 'ten', 'b', 'd', 'ten'], '2nd Attempts': [10, 12, 15, 14, 0],
 '3rd Attempts': [10, 10, 9, 11, 10]}
a = pd.DataFrame(a)
print (a)
   2nd Attempts  3rd Attempts Attempts
0            10            10        a
1            12            10      ten
2            15             9        b
3            14            11        d
4             0            10      ten

mask = (a.values == 'ten').any(1)
print (mask)
[False  True False False  True]

print (a[mask])
   2nd Attempts  3rd Attempts Attempts
1            12            10      ten
4             0            10      ten
<div class="container">
<!--search criteria-->
<div class="alert alert-info">
<form method="post" class="form-inline" action="<?php echo base_url();?>/admin/area/index" id="form_search">
        <div class="pull-left" style="color:#666;">Show Results:&nbsp;&nbsp;
    <select name="limit" id="limit" style="width:50px;">
          <option value="10"<?php if(10==$limit_selected) echo "selected";?>>10</option>
          <option value="20" <?php if(20==$limit_selected) echo "selected";?>>20</option>
          <option value="30" <?php if(30==$limit_selected) echo "selected";?>>30</option>
          <option value="40" <?php if(40==$limit_selected) echo "selected";?>>40</option>
          <option value="50" <?php if(50==$limit_selected) echo "selected";?>>50</option>
        </select>
    </div>
<label style="color:#666;">&nbsp;&nbsp;Search By City:&nbsp;&nbsp;</label>
<select id="cityId" name="cityId" >
               <option value='0'> --All-- </option>
                   <?php 
                  foreach($cityOptionList as $city) {  ?>
                    <option value="<?php echo $city['id']?>" <?php if($city['id']==$cityId) echo "selected";?>><?php echo $city['name']?></option>
                  <?php } ?> 
              </select>
</form>
</div>
<!--search criteria end-->
    <table class="table table-striped table-bordered bootstrap-datatable">
    <thead>
        <tr>
            <th>Sl.no</th>
            <th>Area Name</th>
            <th>City</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        <?php $i=1; foreach($areaList as $area) {?>
        <tr>
            <td><?php echo $i;?></td>
            <td class="center"><?php echo $area->areaName;?></td>
            <td class="center"><?php echo $area->city_name;?></td>
            <td class="center">
            <a  href="<?php echo base_url();?>admin/area/edit/<?php echo $area->areaId ; ?>">
            <i class="icon-edit" style="font-size:16px;"></i></a>
        <!--    <a  href="#" data-id="<?php echo $area->areaId;?>">
                <i class="icon-trash" style="font-size:16px;"></i></a>-->
            </td>
        </tr>
        <?php $i++; } ?>
    </tbody>
</table>   
<?php echo $links;?>            
</div>
<!--/container--> 
<!--=== End Content Part ===-->
<script>
$(function() {
   $("#cityId").change(function() {
     $("#form_search").submit();
   });

    $("#limit").change(function() {
     $("#form_search").submit();
   });
  /* 
   $(".pagination").click(function(){
          alert("hi");
         $("#form_search").submit();
   })
   */
 });
</script>
相关问题