将Pandas列内容与其他原始列一起添加到新列 [英] Pandas column content to new columns, with other original columns
本文介绍了将Pandas列内容与其他原始列一起添加到新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的表格,我想用它制作一个新表格(使用颜色"列中的值).
A table like below, and I want to make a new table from it (using the values in the 'Color' column).
我尝试过:
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])
"df_1"看起来不错,但"final_df"比预期的要长得多.
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.
推荐答案
使用 DataFrame.join
和
查看全文