【问题标题】:Deleting all rows in a pandas dataframe below a row with a specific column value删除具有特定列值的行下方的熊猫数据框中的所有行
【发布时间】:2021-01-07 15:17:56
【问题描述】:

我有一个 Pandas 数据框,在最后几行中有额外的数据。我需要识别具有特定列值的行并删除从该行开始及以下的所有行。

数据框示例:

Mod            Day           Initials
 1            9/4/18            AV
 2            4/20/19           AV
 3            7/18/17           AV
 4            12/1/13           AV
Program       Title           Amount
Axis          Axis Gig         $35
Rex           Rex Gig          $75
DOM           Triple Z         $15

所以,我想确定数据框在“日”列中的“标题”位置,并删除该行及其下方的所有行。

【问题讨论】:

    标签: pandas dataframe filter


    【解决方案1】:

    您可以将布尔索引与 df.where 一起使用

    s = """Mod,Day,Initials
    1,9/4/18,AV
    2,4/20/19,AV
    3,7/18/17,AV
    4,12/1/13,AV
    Program,Title,Amount
    Axis,Axis Gig,$35
    Rex,Rex Gig,$75
    DOM,Triple Z,$15"""
    
    df = pd.read_csv(StringIO(s))
    
    # select where your col equals 'Title' then fill all other values with nan
    # forward fill all nan values after title and the use boolean indexing
    new_df = df[df['Day'].where(df['Day'] == 'Title', np.nan).ffill() != 'Title']
    
      Mod      Day Initials
    0   1   9/4/18       AV
    1   2  4/20/19       AV
    2   3  7/18/17       AV
    3   4  12/1/13       AV
    

    【讨论】:

    • 我收到以下错误:IndexError: single positional indexer is out-of-bounds
    【解决方案2】:

    您可以找到索引并只保留该行之前的所有内容

    df = df[0:df[df['Day'] == 'Title'].index[0]]
    df
    

    输出

      Mod      Day Initials
    0   1   9/4/18       AV
    1   2  4/20/19       AV
    2   3  7/18/17       AV
    3   4  12/1/13       AV
    

    【讨论】:

    • 我收到以下错误:IndexError: single positional indexer is out-of-bounds
    • 我无法重现该错误。我不知道您的数据框中有什么不同。我使用您提供的数据框作为您的数据。
    猜你喜欢
    • 2019-11-12
    • 2013-06-29
    • 1970-01-01
    • 2015-03-29
    • 2019-03-29
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多