首页 > 解决方案 > How to merger same column values based on other column values?

问题描述

How to add a new column in pandas dataframe based on other column values , for example, Id columns has value and column Value has different values for Id, it needs to be merged as in the output.

df:

# dictionary of lists 
data = {'Id':["A", "A", "B", "B","B", "C", "D","E","E", "F", "G","G"], 
        'Value': ["10$", "2$", "30%", "43%", "12$", "43$", "27$", "40%" , 
                     "18$",np.nan,np.nan,"89%"]}

df = pd.DataFrame(data)
print(df)

    Id  Value
0   A   10$
1   A   2$
2   B   30%
3   B   43%
4   B   12$
5   C   43$
6   D   27$
7   E   40%
8   E   18$
9  F     nan
10 G    nan
11 G    89%

output :

 Id Value
0   A   10$, 2$
1   B   30%,43%,12$
3   C   43$
4   D   27$
5   E   40%,18$
6   F    nan
7   G    89%

标签: pythonpython-3.xpandaspython-2.7

解决方案


df.groupby('Id')['Value'].apply(', '.join).reset_index()

print(df)

Output:


   Id   Value
0   A   S1, S2
1   B   S3, S3, S5
2   C   S6
3   D   S7
4   E   S8, S9

推荐阅读