【问题标题】:Creating Dummy Variables from String Column从字符串列创建虚拟变量
【发布时间】:2018-08-17 18:32:15
【问题描述】:

我有一个看起来像这样的 pandas 数据框 (N = 1485):

ID          Intervention
1           Blood Draw, Flushed, Locked
1           Blood Draw, Port De-Accessed, Heparin-Locked, Tubing Changed
1           Blood Draw, Flushed
2           Blood return Verified, Flushed
2           Cap Changed
3           Port De-Accessed

我希望能够在每个逗号之前对每个字符串进行虚拟编码,使其看起来类似于:

ID          Blood Draw          Flushed          Locked      ....
1              Yes                Yes             Yes
1              Yes                No              No
...

谢谢!

【问题讨论】:

标签: python pandas data-structures data-science dummy-variable


【解决方案1】:

您可以使用pd.Series.str.get_dummies 和字典映射:

d = {1: 'yes', 0: 'no'}
res = df.join(df.pop('Intervention').str.get_dummies(', ').applymap(d.get))

在我看来,最好将其转换为仅用于显示目的的字符串。布尔值在布尔系列中更有效地保存和操作。

结果

print(res)

   ID Blood Draw Blood return Verified Cap Changed Flushed Heparin-Locked  \
0   1        yes                    no          no     yes             no   
1   1        yes                    no          no      no            yes   
2   1        yes                    no          no     yes             no   
3   2         no                   yes          no     yes             no   
4   2         no                    no         yes      no             no   
5   3         no                    no          no      no             no   

  Locked Port De-Accessed Tubing Changed  
0    yes               no             no  
1     no              yes            yes  
2     no               no             no  
3     no               no             no  
4     no               no             no  
5     no              yes             no  

设置

df = pd.DataFrame({'ID': [1, 1, 1, 2, 2, 3],
                   'Intervention': ['Blood Draw, Flushed, Locked',
                                    'Blood Draw, Port De-Accessed, Heparin-Locked, Tubing Changed',
                                    'Blood Draw, Flushed', 'Blood return Verified, Flushed',
                                    'Cap Changed', 'Port De-Accessed']})

【讨论】:

    【解决方案2】:

    您可以尝试以下方法:

    for event in ['Blood Draw', 'Flushed', 'Locked']:
        df[event] = df['Intervention'].str.contains(event)
    

    这将为您提供True/False,而不是'Yes'/'No',后者在您进行后期处理时可能更有用。

    【讨论】:

      【解决方案3】:
      import numpy as np
      df1=df['Intervention'].str.split(',', expand=True)  
      df2=df1.replace(np.nan, '', regex=True) # Replacing None with blank data
      pd.concat([pd.get_dummies(df2[col]) for col in df2], axis=1, keys=df2.columns)  # Creates dummies for all the columns 
      

      要执行上述步骤,请过滤Intervention 列,执行此过程并加入原始数据框,以便虚拟语句起作用(为所有列创建虚拟)。

      【讨论】:

      • 如果这是您想要的,请告诉我。
      猜你喜欢
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 2015-05-20
      • 2015-08-11
      • 2019-03-02
      • 2011-03-24
      • 2023-03-27
      相关资源
      最近更新 更多