【问题标题】:Dropping columns before a matching string in the following column在以下列中的匹配字符串之前删除列
【发布时间】:2022-12-12 21:43:53
【问题描述】:

是否有可能直接删除 pandas Dataframe 中匹配字符串之前的所有列。例如。如果我的第 8 列包含字符串 'Matched' 我想删除第 0 到 7 列?

【问题讨论】:

    标签: python pandas dataframe drop


    【解决方案1】:

    好吧,您没有提供任何信息在哪里以及如何寻找“匹配”,但假设整数 col_num 包含匹配列的编号:

    df.drop(columns=df.columns[0:col_num],inplace=True)
    

    会做下降

    【讨论】:

    • 是的。找到 col_num 是我的实际问题。假设我的 df 有 15 列,所有这些都是未命名的。在这 15 个中,只有一列(任何行)的值为“匹配”
    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 2015-08-04
    • 2015-12-25
    相关资源
    最近更新 更多