简体   繁体   English

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

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

How can we split month column into different columns?我们如何将月份列拆分为不同的列?

Sample data:样本数据:

    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

Need an output like需要一个 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

You can use get_dummies with add missing months by DataFrame.reindex , then rename columns and add to original DataFrame :您可以使用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.

相关问题 如何使用python根据一列将整个数据集分为4个范围 - How to split the whole dataset into 4 range based on one column using python 如何在Python中将单行拆分为多列? - how split single row to multiple column in python? 如何将正则表达式结果拆分为多列(Python) - How to split the regex result into multiple column (Python) 如何在python中将数据框中的一列拆分为多列? - How to split a column in a dataframe into multiple columns in python? 如何将 csv 文件中的列拆分为 python jupyter 中的多列? - How to split a column in csv file into multiple column in python jupyter? Python:如何在数据框中将列拆分为多列并使用动态列命名 - Python:how to split column into multiple columns in a dataframe and with dynamic column naming 如何使用 python 将 excel 的一列中的数据拆分为多列 - How to Split data in one column of excel into multiple column using python 如何在python中使用标题将一列拆分为多列? - how to split one column to multiple column with header in python? 如何在不影响数据集本身的情况下将 aa 字符串数据拆分为同一列/单列(Python Pandas)? - How to split a a string data into the same column / single column - without affecting the dataset itself (Python Pandas)? Python-如何使用多个分隔符拆分列值 - Python - How to split column values using multiple separators
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM