【问题标题】:How to extract two specific characters of a column name and attach them to another column name?如何提取列名的两个特定字符并将它们附加到另一个列名?
【发布时间】:2019-09-17 08:04:29
【问题描述】:

我有一个包含 1000+ 列的数据集。大约 100 列包含文本:insured。对于这些列中的每一列,右侧的两列中有一列包含字符“3%”或“4%”。我需要做的是提取子字符串“3%”或“4%”并将其添加到包含单词insured 的列中,例如:insured 3%

到目前为止,我有以下代码:

# Find all columns containing the word 'Insured'
insured_cols = [col for col in df.columns if 'Insured' in col]

# Get the index of these columns
insured_index = [df.columns.get_loc(c) for c in insured_cols if c in df]

# Get the index of the columns that I want to extract either '3%' or '4%' from
percentage_index = [x + 2 for x in insured_index]

# Get dataframe of these columns
percentage_cols = page.iloc[:,percentage_index]

下一步是从percentage_cols 中提取子字符串“3%”或“4%”,并将其添加到insured cols 的列名中。

我希望我的问题足够清楚,如果需要进一步澄清,请告诉我。

【问题讨论】:

    标签: python substring extract


    【解决方案1】:

    如果您可以粘贴数据框的部分列列表会更好。根据我的理解,我生成的虚拟列实现了您想要的解决方案

    cols = "column1 insured x y jpt3%  column2 column3 insured z p jjj4%".split()
    df = pd.DataFrame(np.zeros((10,len(cols))), columns=cols)
    
    new_columns = list(df.columns)
    for i, col in enumerate(df.columns):
        if "insured" in col:
            percent_text = "3%" if df.columns[i+3].find("3%")>-1 else "4%"
            new_columns[i] = col + percent_text
    
    df.columns = new_columns
    df
    
    

    【讨论】:

    • 将 new_columns[i] = col+" 3%" 更改为 new_columns[i] = col+ " " + percent_text 然后它工作了。非常感谢!
    【解决方案2】:

    这应该可行,documentation:

        indexes = numpy.where(percentage_cols=='3%')
        for index in indexes
            insured cols.append(percentage_cols[index])
            np.delete(percentage_cols, percentage_cols[index], axis=0)
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2020-06-14
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多