【问题标题】:In python how split the dataset column to multiple column在python中如何将数据集列拆分为多列
【发布时间】:2021-04-26 16:01:21
【问题描述】:

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

样本数据:

    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

需要类似的输出

    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

【问题讨论】:

  • 新列中没有数据?
  • 是的,也需要像布尔值、0 和 1 这样的数据,还需要对其进行计数 - 就像在特定的(一月、二月 ..)月份一样,现在很多人都加入了 @jezrael
  • 您可以将此数据添加到预期输出吗?
  • 当然,我会尝试添加数据@jezrael

标签: python python-3.x pandas data-science


【解决方案1】:

您可以使用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  

【讨论】:

  • 从上述数据中,如何才能使 mumbai 为 1 而其他所有为 0? @jezrael
猜你喜欢
  • 1970-01-01
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 2013-08-04
  • 2019-03-01
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
相关资源
最近更新 更多