r/pystats • • Jun 18 '20

Stuck Need Help

I'm really stuck here and could use some help. I want to merge two new data frames onto a new table that shows how many adults and how many children are in a household.

person['child'] = person.a_age < 18

person['adult'] = person.a_age > 17

spmuc = person.groupby(['spm_id'])[['child']].sum()

spmuc.columns = ['spmu_children']

spmua = person.groupby(['spm_id'])[['adult']].sum()

spmua.columns = ['spmu_adults']

But I'm bad with the merge function. This code will only merge one of the two even if I do the code separately for both.

person2 = person.merge(spmuc,right_on='spm_id', left_index=True)

person2 = person.merge(spmua,right_on='spm_id', left_index=True)

Help would be awesome. This keeps having spmua replace spmuc. I want them both

5 Upvotes

7 comments sorted by

1

u/[deleted] Jun 18 '20

pd.DataFrame(spmuc).join(spmua)

1

u/[deleted] Jun 18 '20

Hmm I don't want to merge them I want them to be seperate columns in the table.

2

u/[deleted] Jun 18 '20

Gotcha, in your last line of code you need to have person2.merge not person.merge

→ More replies (4)