2021
03-30
03-30
pandas 实现某一列分组,其他列合并成list
pandas列转换为字典,但将相同第一列(键)的所有值合并为一个键形式一:importpandasaspd#datadata=pd.DataFrame({'column1':['key1','key1','key2','key2'],'column2':['value1','value2','value3','value3']})print(data)#Groupeddictdata_dict=data.groupby('column1').column2.apply(list).to_dict()print(data_dict)输出结果:column1column20key1value11key1value22key2value33key2value3{...
继续阅读 >