Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you join multiple rows into one row in pandas?

I have a list that I'm trying to add to a dataframe. It looks something like this:

list_one = ['apple','banana','cherry',' ', 'grape', 'orange', 'pineapple','']

If I add the list to a dataframe, using df = pd.DataFrame({'list_one':list_one}) it'll look like this:

       list_one
   -------------
   0   apple
   1   banana 
   2   cherry
   3  
   4   grape
   5   orange
   6   pineapple
   7  

I want the combine some of the rows into one row, so that the dataframe looks something like this:

       list_one
   -----------------------------
   0   apple, banana, cherry 
   1   grape, orange, pineapple

Is there a simple way to do this?

Thank you for taking the time to read my question and help in any way you can.

like image 927
shorttriptomars Avatar asked Oct 01 '21 05:10

shorttriptomars


People also ask

How do I merge rows in pandas?

To merge rows within a group together in Pandas we can use the agg(~) method together with the join(~) method to concatenate the row values.

What is the difference between merge join and concatenate in pandas?

The main difference between merge & concat is that merge allow you to perform more structured "join" of tables where use of concat is more broad and less structured.

How to merge multiple DataFrames at once in pandas?

You can use the following syntax to merge multiple DataFrames at once in pandas: import pandas as pd from functools import reduce #define list of DataFrames dfs = [df1, df2, df3] #merge all DataFrames into one final_df = reduce (lambda left,right: pd.merge(left,right,on= ['column_name'], how='outer'), dfs)

How to create grouping rows in pandas?

For grouping rows in Pandas, we will start with creating a pandas dataframe first. Now, create a grouping object, means an object that represents that particular grouping.

How do I drop multiple rows in a Dataframe in pandas?

How do you drop multiple rows in Pandas? Pandas provides with .drop () function to delete/drop either rows (axis=0) or columns (axis=1). Syntax : DataFrame.drop (labels=None, axis=0, index=None, columns=None, level=None, inplace=False, errors='raise') To delete multiple rows, we need to access the rows by index.

How to merge two DataFrames in row axis only?

Append is very useful when you want to merge two DataFrames in row axis only. This means that instead of matching data on their columns, we want a new DataFrame that contains all the rows of 2 DataFrames.


Video Answer


1 Answers

Create mask for match words by Series.str.contains, invert by ~ and crate groups by Series.cumsum, filter only matched rows and pass to GroupBy.agg with join function:

m = df['list_one'].str.contains('\w+')
df = df[m].groupby((~m).cumsum(), as_index=False).agg(', '.join)
print (df)
                   list_one
0     apple, banana, cherry
1  grape, orange, pineapple
like image 120
jezrael Avatar answered Oct 24 '22 17:10

jezrael