df.groupby('columns')。apply(''。join()),将所有单元格连接到一个字符串

时间:2019-07-31 11:54:14

标签: pandas

df.groupby('columns').apply(''.join()),将所有单元格连接到一个字符串。

这是针对初级dataprocessor的用户。过去,我尝试过很多方法。

import pandas as pd
data = {'key':['a','b','c','a','b','c','a'], 'profit': 
[12,3,4,5,6,7,9],'income':['j','d','d','g','d','t','d']}
df = pd.DataFrame(data)
df = df.set_index(‘key’)
#df2 is expected result
data2 = {'a':['12j5g9d'],'b':['3d6d'],'c':['4d7t']}
df2 = pd.DataFrame(data2)
df2 = df2.set_index(‘key’)

2 个答案:

答案 0 :(得分:0)

可以通过多种方式解决此问题:

首先通过串联profitincome列添加一个额外的列。

import pandas as pd
data = {'key':['a','b','c','a','b','c','a'], 'profit': 
[12,3,4,5,6,7,9],'income':['j','d','d','g','d','t','d']}
df = pd.DataFrame(data)
df = df.set_index('key')
df['profinc']=df['profit'].astype(str)+df['income']

1)使用sum

df2=df.groupby('key').profinc.sum()

2)使用applyjoin

df2=df.groupby('key').profinc.apply(''.join)

以上两种结果均相同:

key
a    12j5g9d
b       3d6d
c       4d7t

答案 1 :(得分:0)

这是一个简单的解决方案,我们首先将整数转换为字符串,然后将利润和收入连接起来,然后最后将所有字符串连接在同一键下:

data = {'key':['a','b','c','a','b','c','a'], 'profit':
    [12,3,4,5,6,7,9],'income':['j','d','d','g','d','t','d']}
df = pd.DataFrame(data)
df['profit_income'] = df['profit'].apply(str) + df['income']
res = df.groupby('key')['profit_income'].agg(''.join)
print(res)


output:
   key
   a    12j5g9d
   b       3d6d
   c       4d7t
   Name: profit_income, dtype: object