Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

split pandas row into multiple rows

def split_dataframe_rows(df,column_selectors):
    # we need to keep track of the ordering of the columns
    def _split_list_to_rows(row,row_accumulator,column_selector):
        split_rows = {}
        max_split = 0
        for column_selector in column_selectors:
            split_row = row[column_selector]
            split_rows[column_selector] = split_row
            if len(split_row) > max_split:
                max_split = len(split_row)
            
        for i in range(max_split):
            new_row = row.to_dict()
            for column_selector in column_selectors:
                try:
                    new_row[column_selector] = split_rows[column_selector].pop(0)
                except IndexError:
                    new_row[column_selector] = ''
            row_accumulator.append(new_row)

    new_rows = []
    df.apply(_split_list_to_rows,axis=1,args = (new_rows,column_selectors))
    new_df = pd.DataFrame(new_rows, columns=df.columns)
    return new_df
Comment

split rows into multiple columns in pandas

df = pd.DataFrame(df.Raw_info.values.reshape(-1, 3), 
                   columns=['Function_name', 'prop1', 'prop2'])

print(df) 
  Function_name            prop1            prop2
0    Function_1  internal_prop_1  external_prop_1
1    Function_2  internal_prop_2  external_prop_2
2    Function_3  internal_prop_3  external_prop_3
Comment

PREVIOUS NEXT
Code Example
Python :: copy list python 
Python :: ms access python dataframe 
Python :: numpy logspace 
Python :: python requests get 
Python :: python all lowercase letters 
Python :: get random float in range python 
Python :: pandas series index of value 
Python :: how to remove spaces in string in python 
Python :: numpy find columns containing nan 
Python :: print p py pyt pyth pytho python in python 
Python :: string to dictionary python 
Python :: dockerfile for django project 
Python :: django queryset last 10 
Python :: Python remove punctuation from a string 
Python :: python drop the first word 
Python :: ravel python 
Python :: moving file in python 
Python :: python convert multidimensional array to one dimensional 
Python :: pygame rotate image 
Python :: discord py fetch channel by id 
Python :: last index in python 
Python :: random torch tensor 
Python :: python multiple inheritance 
Python :: print 2 decimal places python 
Python :: plotly graph object colorscale 
Python :: python typing effect 
Python :: python http request params 
Python :: seaborn and matplotlib Setting the xlim and ylim python 
Python :: pygame.draw.rect() 
Python :: odoo change admin password from database 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =