我正在尝试对字符串进行分组和连接。我想说的是,我想在串联字符串中包含groupby列。
我已经尝试过下面的代码,并且可以使用!但我担心我这样做的步骤太多。
# Create dataframe
d = {'ORDER_ID': ['order-1','order-2','order-3','order-4','order-5','order-6'], 'pillar_range': ['100','100','100','200','200','300'],'NOTES': ["wet","wet weather","flooded","fine","okay","perfect"]}
df = pd.DataFrame(d)
# Create a concatinated column I want to groupby concatenate in the final step
df['concat_col'] = df['ORDER_ID']+':'+df['NOTES']
# Only select the groupby column and my newly created concatenated column
df = df[['pillar_range','concat_col']]
# Create the final output
df = df.groupby('pillar_range').agg(lambda x: ' | '.join(set(x)))
答案 0 :(得分:1)
好像您将代码切成一行
(df['ORDER_ID']+':'+df['NOTES']).groupby(df['pillar_range']).agg(':'.join)
Out[580]:
pillar_range
100 order-1:wet:order-2:wet weather:order-3:flooded
200 order-4:fine:order-5:okay
300 order-6:perfect
dtype: object