Merge Multiple Rows to Single Row - Pandas

How to combine multiple rows into a single row with pandas

You can use groupby and apply function join :

print df.groupby('value')['tempx'].apply(' '.join).reset_index()
value tempx
0 1.5 picture1 picture555 picture255 picture365 pict...

Pandas: How to merge all rows into a single row?

Try this:

df = pd.DataFrame({'text': [', '.join(df['text'].str.strip('"').tolist())]})

Output:

>>> df
text
0 abc, def, ghi, jkl

Combine multiple rows into single row in Pandas Dataframe

Use if ordering is important GroupBy.agg with lambda function and remove duplicates by dictionary:

df1=df.groupby('ID').agg(lambda x: ','.join(dict.fromkeys(x.astype(str)).keys())).reset_index()

#another alternative, but slow if large data
#df = df.groupby('ID').agg(lambda x: ','.join(x.astype(str).unique())).reset_index()
print (df1)
ID Name City Email \
0 1 Ted,Josh Chicago,Richmond abc@gmail.com
1 2 John,Mike Seattle,Berkley 123@gmail.com,4723@gmail.com

Phone Country
0 132321,435324 USA
1 322421 USA

If ordering is not important use similar solution with removed duplicates by sets:

df2 = df.groupby('ID').agg(lambda x: ','.join(set(x.astype(str)))).reset_index()
print (df2)
ID Name City Email \
0 1 Josh,Ted Richmond,Chicago abc@gmail.com
1 2 John,Mike Berkley,Seattle 4723@gmail.com,123@gmail.com

Phone Country
0 435324,132321 USA
1 322421 USA

Merge multiple rows to single row

If you're wanting to merge dictionaries then use this example

df = pd.DataFrame()
df["dicts"] = [{"this":"is"}, {"a":"collection"}, {"of":"dicts", "the":"end"}]

solution

import itertools
dict(itertools.chain.from_iterable(d.items() for d in df["dicts"]))

This gives you

{'this': 'is', 'a': 'collection', 'of': 'dicts', 'the': 'end'}

Merge multiple rows to single row - pandas

Convert column to list and call DataFrame constructor:

df = pd.DataFrame({'names': [df['names'].tolist()]})
print (df)
names
0 [{'name': 'Ashwin', 'age': '26'}, {'name': 'Al...

If values are strings and need list of dictionaries:

import ast

df = pd.DataFrame({'names': [df['names'].apply(ast.literal_eval).tolist()]})
print (df)
names
0 [{'name': 'Ashwin', 'age': '26'}, {'name': 'Al...


Related Topics



Leave a reply



Submit