Skip to content Skip to sidebar Skip to footer

Pandas Column Content To New Columns, With Other Original Columns

A table like below, and I want to make a new table from it (using the values in the 'Color' column). I've tried: import pandas as pd import functools data = {'Seller': ['Mike','M

Solution 1:

Use DataFrame.join with append=True in DataFrame.set_index for add new column to index:

df_1 = df.join(df.set_index('Color', append=True)['Color_date'].unstack())
print (df_1)
  Seller   Code        From  Color_date   Color Delivery        Blue  \
0   Mike  9QBR1  2020-01-03  2020-02-14    Blue    Nancy  2020-02-14   
1   Mike  9QBR1  2020-01-03  2020-02-14     Red    Nancy         NaN   
2   Mike  9QBW2  2020-01-03  2020-05-18     Red     Kate         NaN   
3   Mike  9QBW2  2020-01-03  2020-05-18    Grey     Kate         NaN   
4  David  9QD1X  2020-01-03  2020-01-04     Red    Lilly         NaN   
5  David  9QD1X  2020-01-03  2020-01-04    Grey    Lilly         NaN   
6   Pete  9QEBO  2020-01-03  2020-03-04    Blue     John  2020-03-04   
7   Pete  9QEBO  2020-01-03  2020-03-13  Orange     John         NaN   
8   Pete  9QEBO  2020-01-03  2020-01-28     Red     John         NaN   

         Grey      Orange         Red  
0         NaN         NaN         NaN  
1         NaN         NaN  2020-02-14  
2         NaN         NaN  2020-05-18  
3  2020-05-18         NaN         NaN  
4         NaN         NaN  2020-01-04  
5  2020-01-04         NaN         NaN  
6         NaN         NaN         NaN  
7         NaN  2020-03-13         NaN  
8         NaN         NaN  2020-01-28  

Post a Comment for "Pandas Column Content To New Columns, With Other Original Columns"