繁体   English   中英

在 python 如何将数据集列拆分为多列

[英]In python how split the dataset column to multiple column

我们如何将月份列拆分为不同的列?

样本数据:

    EmployeeId  City     join_month
0   001        Mumbai        1
1   001        Bangalore     3
2   002        Pune          2
3   002        Mumbai        6
4   003        Delhi         9
5   003        Mumbai        12
6   004        Bangalore     11
7   004        Pune          10
8   005        Mumbai         5

需要一个 output 之类的

    EmployeeId  City     join_month    join_month_jan    jan_count  
0   001        Mumbai        1                 1/True         1 
1   001        Bangalore     3                 0/False      
2   002        Pune          2                 0/False          
3   002        Mumbai        6
4   003        Delhi         9
5   003        Mumbai        12
6   004        Bangalore     11
7   004        Pune          10
8   005        Mumbai         5

您可以使用get_dummies通过DataFrame.reindex添加缺少的月份,然后rename列并添加到原始DataFrame

look_up = {1: 'jan', 2: 'feb', 3: 'mar', 4: 'apr', 5: 'may',
           6: 'jun', 7: 'jul', 8: 'aug', 9: 'sep',
           10: 'oct', 11: 'nov', 12: 'dec'}


df1 = (pd.get_dummies(df['join_month'])
         .reindex(range(1,13), axis=1, fill_value=0)
         .rename(columns=look_up)
         .add_prefix('join_month_'))
# print (df1)


df = df.join(df1)
print (df)

 EmployeeId       City  join_month  join_month_jan  join_month_feb  \
0        001     Mumbai           1               1               0   
1        001  Bangalore           3               0               0   
2        002       Pune           2               0               1   
3        002     Mumbai           6               0               0   
4        003      Delhi           9               0               0   
5        003     Mumbai          12               0               0   
6        004  Bangalore          11               0               0   
7        004       Pune          10               0               0   
8        005     Mumbai           5               0               0   

   join_month_mar  join_month_apr  join_month_may  join_month_jun  \
0               0               0               0               0   
1               1               0               0               0   
2               0               0               0               0   
3               0               0               0               1   
4               0               0               0               0   
5               0               0               0               0   
6               0               0               0               0   
7               0               0               0               0   
8               0               0               1               0   

   join_month_jul  join_month_aug  join_month_sep  join_month_oct  \
0               0               0               0               0   
1               0               0               0               0   
2               0               0               0               0   
3               0               0               0               0   
4               0               0               1               0   
5               0               0               0               0   
6               0               0               0               0   
7               0               0               0               1   
8               0               0               0               0   

   join_month_nov  join_month_dec  
0               0               0  
1               0               0  
2               0               0  
3               0               0  
4               0               0  
5               0               1  
6               1               0  
7               0               0  
8               0               0  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM