另一种执行此方法的方法是使用groupy
,apply(list)
然后使用转换列表值到单独的列Series.values.tolist()
# Groupby and get the values in a list per unique value of the Date column
df = df.groupby('Date').String.apply(list).reset_index()
Date String
0 2016-06-30 [d, e, f]
1 2016-08-01 [a, b, c]
# Convert the values from the list to seperate columns and after that drop the String column
df[['Column1', 'Column2', 'Column3']] = pd.DataFrame(df.String.values.tolist(), index=df.index)
df.drop('String', axis=1, inplace=True)
Date Column1 Column2 Column3
0 2016-06-30 d e f
1 2016-08-01 a b c