Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

expand pandas dataframe into separate rows

def explode(df, lst_cols, fill_value='', preserve_index=False):
    # make sure `lst_cols` is list-alike
    if (lst_cols is not None
        and len(lst_cols) > 0
        and not isinstance(lst_cols, (list, tuple, np.ndarray, pd.Series))):
        lst_cols = [lst_cols]
    # all columns except `lst_cols`
    idx_cols = df.columns.difference(lst_cols)
    # calculate lengths of lists
    lens = df[lst_cols[0]].str.len()
    # preserve original index values    
    idx = np.repeat(df.index.values, lens)
    # create "exploded" DF
    res = (pd.DataFrame({
                col:np.repeat(df[col].values, lens)
                for col in idx_cols},
                index=idx)
             .assign(**{col:np.concatenate(df.loc[lens>0, col].values)
                            for col in lst_cols}))
    # append those rows that have empty lists
    if (lens == 0).any():
        # at least one list in cells is empty
        res = (res.append(df.loc[lens==0, idx_cols], sort=False)
                  .fillna(fill_value))
    # revert the original index order
    res = res.sort_index()
    # reset index if requested
    if not preserve_index:        
        res = res.reset_index(drop=True)
    return res
Comment

Split data-frame using Rows

# splitting dataframe by row index
df_1 = df.iloc[:1000,:]
df_2 = df.iloc[1000:,:]
print("Shape of new dataframes - {} , {}".format(df_1.shape, df_2.shape))
Comment

PREVIOUS NEXT
Code Example
Python :: post from postman and receive in python 
Python :: get index of first true value numpy 
Python :: feature engineering data preprocessing 
Python :: how to compare list and int in python 
Python :: django migrations 
Python :: check boolean python 
Python :: python tkinter programming project ideas 
Python :: how to get all values from class in python 
Python :: list count python 
Python :: range in python 
Python :: panda lambda function returns dataframe 
Python :: convert method to str python 
Python :: generate python 
Python :: python empty list 
Python :: add vertical line in plot python 
Python :: python regex (d)(?=d1) 
Python :: negative slicing in python list 
Python :: += in python 
Python :: polymorphism in python 
Python :: python split() source code 
Python :: How to find the maximum subarray sum in python? 
Python :: os module in python 
Python :: UserWarning: X does not have valid feature names, but LinearRegression was fitted with feature names 
Python :: keras transfer learning 
Python :: streamlit cheatsheet 
Python :: __all__ python 
Python :: python calling method from constructor 
Python :: python number type 
Python :: import os in python 
Python :: handling exceptions 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =