【问题标题】:combining columns to make new one in pandas合并列以在 pandas 中创建新列
【发布时间】:2021-01-21 22:32:24
【问题描述】:

我有以下数据,想组合以下列来创建一个新的二进制,no = 0 和 yes = 1。我想组合到新列中的特征是:

曾经告诉过你患有充血性心力衰竭,曾经告诉过你患有冠心病, 曾经告诉过你有心绞痛/心绞痛,曾经告诉过你心脏病发作,曾经告诉过你中风

Age in years at screening                   15881 non-null float64
Race/Hispanic origin                        15881 non-null object
Ratio of family income to poverty           15881 non-null float64
Gender                                      15881 non-null object
year                                        15881 non-null object
60 sec. pulse (30 sec. pulse * 2)           15881 non-null float64
Weight (kg)                                 15881 non-null float64
Standing Height (cm)                        15881 non-null float64
Waist Circumference (cm)                    15881 non-null float64
Arm Circumference (cm)                      15881 non-null float64
Ever told had congestive heart failure      15881 non-null object
Ever told you had coronary heart disease    15881 non-null object
Ever told you had angina/angina pectoris    15881 non-null object
Ever told you had heart attack              15881 non-null object
Ever told you had a stroke                  15881 non-null object
Do you now smoke cigarettes?                15881 non-null object
Doctor told you have diabetes               15881 non-null object
How often drink alcohol over past 12 mos    15881 non-null float64
Sodium (mmol/L)                             15881 non-null float64
Cholesterol, refrigerated serum (mg/dL)     15881 non-null float64
avg_systolic_blood_pres                     15881 non-null float64
avg_diastolic_blood_pres                    15881 non-null float64

我还担心我最终可能会得到比原始数据集更多的数据(15881 行,22 列)

【问题讨论】:

    标签: python pandas machine-learning binary classification


    【解决方案1】:

    假设您的数据是这种格式(表格是转置,零是虚拟变量)

                                              15881  15882  15883
    Q                                                            
    Age_in_years_at_screening                     0      0      0
    Race/Hispanic_origin                          0      0      0
    Ratio_of_family_income_to_poverty             0      0      0
    Gender                                        0      0      0
    year                                          0      0      0
    60_sec._pulse_(30_sec._pulse_*_2)             0      0      0
    Weight_(kg)                                   0      0      0
    Standing_Height_(cm)                          0      0      0
    Waist_Circumference_(cm)                      0      0      0
    Arm_Circumference_(cm)                        0      0      0
    Ever_told_had_congestive_heart_failure    False  False  False
    Ever_told_you_had_coronary_heart_disease   True  False  False
    Ever_told_you_had_angina/angina_pectoris   True  False   True
    Ever_told_you_had_heart_attack             True  False   True
    Ever_told_you_had_a_stroke                 True  False   True
    Do_you_now_smoke_cigarettes?                  0      0      0
    Doctor_told_you_have_diabetes                 0      0      0
    How_often_drink_alcohol_over_past_12_mos      0      0      0
    Sodium_(mmol/L)                               0      0      0
    Cholesterol_refrigerated_serum_(mg/dL)        0      0      0
    avg_systolic_blood_pres                       0      0      0
    avg_diastolic_blood_pres                      0      0      0
    

    您可以指定感兴趣的问题并对其进行操作

    questions = ['Ever_told_had_congestive_heart_failure',
    'Ever_told_you_had_coronary_heart_disease',
    'Ever_told_you_had_angina/angina_pectoris',
    'Ever_told_you_had_heart_attack',
    'Ever_told_you_had_a_stroke']
    
    df["Ever_told_combined"] = df[questions].apply(lambda row: np.logical_or.reduce(row), axis=1)
    

    将“Ever_told_combined”列添加到数据框

    15881     True
    15882    False
    15883     True
    dtype: bool
    

    【讨论】:

      【解决方案2】:

      如果您想创建一个新列,如果任何列有“1”,则返回“true”,您可以执行以下操作:

      df = pd.DataFrame({'congestive': np.random.randint(2, size=10),
                         'coronary': np.random.randint(2, size=10)})
      
      
      
      df['new'] = (df['congestive'] == 1) | (df['coronary'] == 1)
      
      Out[66]: 
         congestive  coronary    new
      0           1         1   True
      1           1         1   True
      2           1         0   True
      3           1         1   True
      4           0         0  False
      5           0         0  False
      6           0         1   True
      7           1         0   True
      8           0         1   True
      9           1         1   True
      

      请参阅Is there a simple way to change a column of yes/no to 1/0 in a Pandas dataframe? 将 True / False 更改为 1 / 0。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-09
        • 2021-04-14
        • 2017-05-06
        • 1970-01-01
        • 2019-08-27
        • 2018-12-20
        • 2016-12-25
        相关资源
        最近更新 更多