修改Dataframe中除最后一行外的组的所有行



我有以下数据帧

data = [['object 1', 'property 1'], ['object 1','property 11'],['object 1','property 111'], ['object 2', 'property 2'], ['object 3', 'property 3'],['object 3','property 33']]    
df = pd.DataFrame(data, columns=['label', 'attribuutLabel'])

我希望每个标签(对象)通过在每个attribuutLabel (properties)上添加逗号来修改所有属性,但最后一行除外。如果对象只有一个属性,则不加逗号。

那么输出应该是这样的:

tbody> <<tr>
labelattribuutLabel
对象1属性1,
对象1属性11,
对象1属性111
对象2属性2
对象3属性3,
对象3属性33

嗯,这比我想象的要难!

但是谢谢你的练习;)

让我们这样做:

#check labels which are present more than one time
lab_count = df.groupby('label')['attribuutLabel'].count().loc[lambda x: x>1]
#considering the result above, select only the labels >1 and add a comma to all the corresponding "property" BUT the last
df1 = df[df['label'].isin(lab_count.index)].groupby('label', group_keys=False)['attribuutLabel'].apply(lambda x: x.iloc[:-1]+",")
#replace/update value from original df with the new values from df1 using .iloc[]
df.iloc[df1.index,1] = df1[:]
df
tbody> <<tr>12435
indexlabelattribuutLabel
0对象1属性1,
对象1属性11日
对象1属性111
3对象2属性2
对象3属性,
对象3属性33

最新更新