python - 删除 pandas 中的列

标签 python pandas

在转置并添加几列后,我创建了一个如下所示的数据框。

初始 df-

                         plan_benefits  value                    plan_benefits_db value_db  valid_flag
0            durable_medical_equipment     20           durable_medical_equipment       40       False
1                                  pcp     45                                 pcp       40       False
2                           specialist     80                          specialist       40       False
3                           diagnostic   7540                          diagnostic       40       False
4                              imaging    300                             imaging       40       False
5                              generic     30                             generic    40500       False
6                      formulary_brand    110                     formulary_brand    40500       False
7                non_preferred_generic  55110               non_preferred_generic    40500       False
8                       emergency_room    350                      emergency_room       40       False
9                   inpatient_facility     20                  inpatient_facility       40       False
10           medical_deductible_single   2000           medical_deductible_single     6000       False
11           medical_deductible_family   4000           medical_deductible_family    12000       False
12  maximum_out_of_pocket_limit_single   7550  maximum_out_of_pocket_limit_single     6650       False
13  maximum_out_of_pocket_limit_family  15100  maximum_out_of_pocket_limit_family    13300       False

最终 df -

  plan_benefits  durable_medical_equipment    pcp  specialist  diagnostic  imaging  generic  formulary_brand  non_preferred_generic  emergency_room  inpatient_facility  medical_deductible_single  medical_deductible_family  maximum_out_of_pocket_limit_single  maximum_out_of_pocket_limit_family plan_name      pdf_name
    valid_flag                         False  False       False       False    False    False            False                  False           False               False                      False                      False                               False                               False   ABCBCBC  adjnajdn.pdf

我执行过的操作 -

    df_repo = df_repo[['plan_benefits', 'valid_flag']].set_index('plan_benefits').transpose()

    df_repo['plan_name'] = 'ABCBCBC'
    df_repo['pdf_name'] = 'adjnajdn.pdf'
    # df_repo = df_repo.drop('plan_benefits', 1)

    print(df_repo.to_string())

我需要删除第一列“plan_benefits”。使用 drop() 时,我收到 KeyError: "['plan_benefits'] not found in axis"

我尝试了多种选项,例如 del df['plan_benefits'] 但没有任何效果。

评论中的答案之后的最终结果 -

 durable_medical_equipment    pcp  specialist  diagnostic  imaging  generic  formulary_brand  non_preferred_generic  emergency_room  inpatient_facility  medical_deductible_single  medical_deductible_family  maximum_out_of_pocket_limit_single  maximum_out_of_pocket_limit_family plan_name      pdf_name
0                      False  False       False       False    False    False            False                  False           False               False                      False                      False                               False                               False   ABCBCBC  adjnajdn.pdf

最佳答案

首先需要删除列名称并创建默认索引:

df = (df_repo[['plan_benefits', 'valid_flag']].set_index('plan_benefits')
              .T.reset_index(drop=True))
df.columns.name = None

或者:

df = (df_repo[['plan_benefits', 'valid_flag']]
        .set_index('plan_benefits')
        .transpose()
        .reset_index(drop=True)
        .rename_axis(None, axis=1))

print (df)
   durable_medical_equipment    pcp  specialist  diagnostic  imaging  generic  \
0                      False  False       False       False    False    False   

   formulary_brand  non_preferred_generic  emergency_room  inpatient_facility  \
0            False                  False           False               False   

   medical_deductible_single  medical_deductible_family  \
0                      False                      False   

   maximum_out_of_pocket_limit_single  maximum_out_of_pocket_limit_family  
0                               False                               False 

最后将 index=False 添加到 DataFrame.to_excel :

df.to_excel('file.xlsx', index=False)
<小时/>

顺便说一句,因为索引被删除,解决方案应该简化:

df = df_repo[['plan_benefits', 'valid_flag']].set_index('plan_benefits').transpose()
df.to_excel('file.xlsx', index=False)

关于python - 删除 pandas 中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57556844/

相关文章:

python - 列名和相应的数据在 python 中不匹配

python - 使用 Pandas 获取 DataFrame 中的每小时数据

python-3.x - 如何根据 Pandas 中的条件过滤时间戳

python - 根据子串匹配列

python - 监督机器学习 : Classify types of clusters of data based on shape and density (Python)

python - 如何在 pyenv 中创建一个 post virtualenv hook 来升级 pip 包

c++ - 在 c 中嵌入 python 时 Numpy 导入失败

python - 如何使用不同的类和导入动态地使用 Python 日志更改文件句柄

python - 检查值是否在元组中的一对值之间?

python - 使用 pandas 使用现有数据框中的唯一元素生成随机数据框