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 set
s:
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
Pandas Update and Add Rows One Dataframe With Key Column in Another Dataframe
How to Write Multiple Images (Subplots) into One Image
Python: Element Is Not Attached to the Page Document
How to Skip Blank Line While Reading CSV File Using Python
Beautifulsoup Findall() Given Multiple Classes
Cv2 Image Error: Error: (-215:Assertion Failed) !Ssize.Empty() in Function 'Cv::Resize'
Replacing Values in a Dataframe for Given Indices
Truth Value of a Series Is Ambiguous. Use A.Empty, A.Bool(), A.Item(), A.Any() or A.All()
Calculate Sklearn.Roc_Auc_Score for Multi-Class
Python: Split a List into Multiple Lists Based on a Subset of Elements
Drop Rows Containing Empty Cells from a Pandas Dataframe
Matrix Flip Horizontal or Vertical
Search and Replace a Line in a File in Python
Maximum Characters That Can Be Stuffed into Raw_Input() in Python
How to Properly Redirect Stdout/Stderr from a Systemd Service on Raspbian