how to combine the data in dataframe

Multi tool use
Multi tool use


how to combine the data in dataframe



I am working with datasets having records in csv file as:


C_id C_Name S_Name Phone
97 [Bedsheet] Shree Collection 77422222
97 [Bedsheet] Shree Collection 77422222
105 [Jeans] Shree Collection 77422222
61 [Kurti] F Fashion 9040645
95 [Lehenga] Shree Collection 77422222
61 [Kurti] F Fashion 9040645
73 [Cotton Kurti] F Fashion 9040645
117 [Earring] Banti 90406459



I want to show the data the data as like :


C_id C_Name S_Name Phone
97,105,95 Bedsheet,Jeans,Lehenga Shree Collection 77422222
61,73 Kurti,Cotton Kurti F Fashion 9040645
117 Earring Banti 90406459



How can this be achieved





Please show what you've tried. And I assume, you are talking about Pandas DataFrames?
– ksbg
Jul 3 at 8:35




1 Answer
1



You can use groupby with agg and a couple of str.join functions. The parts where you need particular care:


groupby


agg


str.join


C_id


str.join


int


str


C_Name


itertools.chain



Here's a working example:


from itertools import chain

agg_funcs = {'C_id': lambda x: ','.join(map(str, x)),
'C_Name': lambda x: ','.join(chain.from_iterable(x))}

res = df.groupby(['S_Name', 'Phone']).agg(agg_funcs).reset_index()

print(res)

S_Name Phone C_id C_Name
0 Banti 90406459 117 Earring
1 F Fashion 9040645 61,61,73 Kurti,Kurti,Cotton Kurti
2 Shree Collection 77422222 97,97,105,95 Bedsheet,Bedsheet,Jeans,Lehenga






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

vfTfpMJ60y11Hn,1jlx,fzy 2Bl7R3s
OGkriCeRBNMiHhhFs

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications