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","Mike","Mike","Mike","David","David","Pete","Pete","Pete"],
'Code' : ["9QBR1","9QBR1","9QBW2","9QBW2","9QD1X","9QD1X","9QEBO","9QEBO","9QEBO"],
'From': ["2020-01-03","2020-01-03","2020-01-03","2020-01-03","2020-01-03","2020-01-03","2020-01-03","2020-01-03","2020-01-03"],
'Color_date' : ["2020-02-14","2020-02-14","2020-05-18","2020-05-18","2020-01-04","2020-01-04","2020-03-04","2020-03-13","2020-01-28"],
'Color' : ["Blue","Red","Red","Grey","Red","Grey","Blue","Orange","Red"],
'Delivery' : ["Nancy","Nancy","Kate","Kate","Lilly","Lilly","John","John","John"]}
df = pd.DataFrame(data)
df_1 = df.set_index([df.index, 'Color'])['Color_date'].unstack()
df_1['Code'] = df['Code']
final_df = functools.reduce(lambda left,right: pd.merge(left,right,on='Code'), [df, df_1])
The "df_1" looks ok but the "final_df" is much longer than expected.
Where went wrong, and how can I correct it? Thank you.
question from:
https://stackoverflow.com/questions/65932602/pandas-column-content-to-new-columns-with-other-original-columns