Skip to content

Instantly share code, notes, and snippets.

@chongwangcc
Last active February 16, 2019 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chongwangcc/4502a70a2c82b5f7c823250b563ad9c6 to your computer and use it in GitHub Desktop.
Save chongwangcc/4502a70a2c82b5f7c823250b563ad9c6 to your computer and use it in GitHub Desktop.
[roupby 如何将相同ID的字符串进行合并 ] #pandas
import pandas as pd
import numpy as np
data = pd.DataFrame({'id':[1,1,1,2,2,2],'value':['A','B','C','D','E','F']})
data1 = data.groupby(by='id')['value'].sum()
"""
输出结果
id
1 ABC
2 DEF
Name: value, dtype: object
"""
#解法一:可以用sum方法,将字符串进行连接
#① 我们可以先将原始数据的value都变成“,A”
data = pd.DataFrame({'id':[1,1,1,2,2,2],'value':['A','B','C','D','E','F']})
data['value'] = data['value'].apply(lambda x:','+ x)
#然后,对其使用sum方法进行字符串相加
data1 = data.groupby(by='id').sum()
"""
此时的输出结果为,value值之前多了“,”
id value
1 ,A,B,C
2 ,D,E,F
"""
#解法二:对分组之后的结果,直接使用apply函数
data1 = data.groupby(by='id').apply(lambda x:[','.join(x['value'])])
"""
就得到了最终的结果:
id value
1 [A,B,C]
2 [D,E,F]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment