【发布时间】: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 的列名中。
我希望我的问题足够清楚,如果需要进一步澄清,请告诉我。
【问题讨论】: