Python 索引选择问题

Python 索引选择问题,python,pandas,dataframe,Python,Pandas,Dataframe,产出: data = DataFrame(np.arange(16).reshape((4, 4)), index=['Ohio', 'Colorado', 'Utah', 'New York'], columns=['one', 'two', 'three', 'four']) 我现在想: 筛选此数据帧并仅输出['three']>5的行 仅输出前3行。。。所以忽略最后一个 我这样写: >>> data one two three

产出:

data = DataFrame(np.arange(16).reshape((4, 4)),
    index=['Ohio', 'Colorado', 'Utah', 'New York'],
    columns=['one', 'two', 'three', 'four'])
我现在想:

  • 筛选此数据帧并仅输出['three']>5的行
  • 仅输出前3行。。。所以忽略最后一个
  • 我这样写:

    >>> data
               one  two  three  four
    Ohio        0    1      2     3
    Colorado    4    5      6     7
    Utah        8    9     10    11
    New York   12   13     14    15
    
    并获取以下错误:

    data.iloc[data['three']>5, [:2]]
    

    你的问题有点不清楚,但我理解的是,你需要科罗拉多州和犹他州在你的最终输出。如果是这种情况,请将代码更改为:

    import pandas as pd
    from pandas import Series, DataFrame
    import numpy as np
    

    试试这个:
    data[data['three']>5][:2]
    刚刚看到@IoaTzimas已经回答了这个问题谢谢你,它让我压力太大了!
    import pandas as pd
    from pandas import Series, DataFrame
    import numpy as np
    
    data[data['three']>5][:2]