Reputation: 9859
q = [{"name":"Mike","age":21, "text": "aaa"},{"name":"Jow","age":22, "text": "bbb"},{"name":"Piter","age":22, "text": "ccc"},{"name":"David","age":25, "text": "ddd"}]
df = pd.DataFrame(q)
result = df["name"].groupby(df['age']).agg(','.join).to_frame()
print(df)
print('---')
print(result)
Output:
$ app.py
age name text
0 21 Mike aaa
1 22 Jow bbb
2 22 Piter ccc
3 25 David ddd
---
name
age
21 Mike
22 Jow,Piter
25 David
But where is my text
column? How to add it in result output?
Upvotes: 1
Views: 670
Reputation: 862441
Your code working with Name
column, so text
column is omitted:
#group column name by column age
result = df["name"].groupby(df['age']).agg(','.join).to_frame()
#more common altearnative - group by column age columns specified in [] after groupby()
result = df.groupby('age')['name'].agg(','.join).to_frame()
print (result)
name
age
21 Mike
22 Jow,Piter
25 David
result = df.groupby('age')['text'].agg(','.join).to_frame()
print (result)
text
age
21 aaa
22 bbb,ccc
25 ddd
#if need specified multiple columns
result = df.groupby('age')['name','text'].agg(','.join)
print (result)
name text
age
21 Mike aaa
22 Jow,Piter bbb,ccc
25 David ddd
#if omit [] proceses all non numeric columns
result = df.groupby('age').agg(','.join)
print (result)
name text
age
21 Mike aaa
22 Jow,Piter bbb,ccc
25 David ddd
Upvotes: 1
Reputation: 744
When you do df["name"]
you are slicing only the "name"
column, so all other columns will "disappear".
I guess that you're trying to do the following:
result = df.groupby('age').agg(','.join)
print(df)
print('---')
print(result)
age name text
0 21 Mike aaa
1 22 Jow bbb
2 22 Piter ccc
3 25 David ddd
---
name text
age
21 Mike aaa
22 Jow,Piter bbb,ccc
25 David ddd
Upvotes: 2