Python 向dataframe添加特定于类别的列和值

Python 向dataframe添加特定于类别的列和值,python,pandas,dataframe,Python,Pandas,Dataframe,我希望根据一些列的相应类别创建特定于类别的列 通过(1)将两个类别分割成两个单独的数据帧,(2)在日期合并两个数据帧(3)删除冗余列(4)创建新列(类别不可知)(4)删除特定于类别的列,我已经大致完成了这一点。你知道一种更有效的方法来进行转化吗?我的代码在示例输入/输出下面 输入: wk start car rims color Autopilot$ Sunroof$ 0 2018-09-09 tesla model x 17 black

我希望根据一些列的相应类别创建特定于类别的列

通过(1)将两个类别分割成两个单独的数据帧,(2)在日期合并两个数据帧(3)删除冗余列(4)创建新列(类别不可知)(4)删除特定于类别的列,我已经大致完成了这一点。你知道一种更有效的方法来进行转化吗?我的代码在示例输入/输出下面

输入:

      wk start  car            rims color   Autopilot$  Sunroof$
0   2018-09-09  tesla model x   17  black   3000         0
1   2018-09-16  tesla model x   14  yellow  3000         0
2   2018-09-23  tesla model x   13  white   3000         0
3   2018-09-09  tesla model 3   19  grey    0            2000
4   2018-09-16  tesla model 3   21  pink    0            2000
理想输出:

     wk       rims-mod3 rims-modx   color-mod3  color-modx  Auto$   roof$
0   2018-09-09  17         0        black       grey        3000    2000
1   2018-09-16  14         19       yellow      pink        3000    2000
2   2018-09-23  13         21       white       NaN         3000    0
我的代码:

import pandas as pd
df = pd.DataFrame({'wk start': ['2018-09-09', '2018-09-16', '2018-09-23','2018-09-09', '2018-09-16'], 
    'car': [ 'tesla model x', 'tesla model x', 'tesla model x','tesla model 3','tesla model 3'],
    'rims': [17,14,13,19,21],
    'color':['black','yellow','white','grey','pink'],
    'Autopilot$':[3000,3000, 3000,0,0],
    'Sunroof$':[0,0,0,2000,2000]})
model3 = df[df['car']=='tesla model 3']
modelx = df[df['car']=='tesla model x']
example = model3.merge(modelx, how='outer',left_on='wk start',right_on='wk start',suffixes=('_model3', '_modelx'))
del example['car_model3']
del example['car_modelx']
example['AUTOPILOT']=example['Autopilot$_model3']+example['Autopilot$_modelx']
example['SUNROOF']=example['Sunroof$_model3']+example['Sunroof$_modelx']
del example['Autopilot$_model3']
del example['Autopilot$_modelx']
del example['Sunroof$_modelx']
del example['Sunroof$_model3']
使用的其他资源包括使用:

df = df.set_index(['wk start','car']).unstack()
df.columns = df.columns.map('_'.join)

df = df.reset_index()

df = df.loc[:, df.fillna(0).ne(0).any()]
print (df)
     wk start  rims_tesla model 3  rims_tesla model x color_tesla model 3  \
0  2018-09-09                19.0                17.0                grey   
1  2018-09-16                21.0                14.0                pink   
2  2018-09-23                 NaN                13.0                 NaN   

  color_tesla model x  Autopilot$_tesla model x  Sunroof$_tesla model 3  
0               black                    3000.0                  2000.0  
1              yellow                    3000.0                  2000.0  
2               white                    3000.0                     NaN  
说明

  • 重塑
  • 通过
    map
    join
    展平列中的多索引
  • 按列索引
  • 使用
    loc
  • 编辑:

    你能解释一下这一行吗df.loc[:,df.fillna(0.ne(0.any)]?我搞不懂它是干什么的?没有任何nan值

    如果使用
    unstack
    ,则可能会丢失一些值,如本示例中所示:

    print (df)
         wk start  rims_tesla model 3  rims_tesla model x color_tesla model 3  \
    0  2018-09-09                19.0                17.0                grey   
    1  2018-09-16                21.0                14.0                pink   
    2  2018-09-23                 NaN                13.0                 NaN   
    
      color_tesla model x  Autopilot$_tesla model 3  Autopilot$_tesla model x  \
    0               black                       0.0                    3000.0   
    1              yellow                       0.0                    3000.0   
    2               white                       NaN                    3000.0   
    
       Sunroof$_tesla model 3  Sunroof$_tesla model x  
    0                  2000.0                     0.0  
    1                  2000.0                     0.0  
    2                     NaN                     0.0  
    
    因此,需要为不包含所有零或带有NAN的所有零的列返回真值(使用
    fillna(0)
    )的原因是什么:

    使用以下选项检查是否至少有一个为真:


    jezrael,你能解释一下这行代码吗?我搞不懂它是干什么的?没有任何
    nan
    值。
    print (df.fillna(0).ne(0))
       wk start  rims_tesla model 3  rims_tesla model x  color_tesla model 3  \
    0      True                True                True                 True   
    1      True                True                True                 True   
    2      True               False                True                False   
    
       color_tesla model x  Autopilot$_tesla model 3  Autopilot$_tesla model x  \
    0                 True                     False                      True   
    1                 True                     False                      True   
    2                 True                     False                      True   
    
       Sunroof$_tesla model 3  Sunroof$_tesla model x  
    0                    True                   False  
    1                    True                   False  
    2                   False                   False  
    
    print (df.fillna(0).ne(0).any())
    wk start                     True
    rims_tesla model 3           True
    rims_tesla model x           True
    color_tesla model 3          True
    color_tesla model x          True
    Autopilot$_tesla model 3    False
    Autopilot$_tesla model x     True
    Sunroof$_tesla model 3       True
    Sunroof$_tesla model x      False
    dtype: bool