Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas split column into multiple columns by delimiter

df[['A', 'B']] = df['AB'].str.split(' ', 1, expand=True)
Comment

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 a column into multiple columns

df[['A', 'B']] = df['AB'].str.split(' ', 1, expand=True)
df['A'], df['B'] = df['AB'].str.split('-', 1).str
Comment

pandas split column into multiple columns

df.Name.str.split(expand=True,)
          0  1
0   Steve   Smith
1   Joe Nadal
2   Roger   Federer
Comment

split coumn of df into multiple dynamic columns

d = [pd.DataFrame(df[col].tolist()).add_prefix(col) for col in df.columns]
df = pd.concat(d, axis=1)

   id0  id1   id2  value0  value1  value2
0   10   10   NaN   apple  orange    None
1   15   67   NaN  banana  orange    None
2   12   34  45.0   apple  banana  orange
Comment

pandas split cell into multiple columns

df[['A', 'B']] = df['AB'].str.split(' ', 1, expand=True)
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

Split Column with delimiter into multiple columns

DECLARE @tt TABLE(i INT IDENTITY,x VARCHAR(8000));
INSERT INTO @tt(x)VALUES('-9;-9;-1;-9;-9;-9;-9;-9;-1;-9;-9;-9;-9;-9;-9;-9;-9;-9;-1;-9;-9;-9;-9;-9;-9;-9;-9;-9;-1;-9;-1;-9;-9;-9;-1;-9;-9;-9;-9;-9;-9;-1;-1;-1;-1;-9;-1;-1;-9;-9;-9;-9;-1;-9;-1;-9;-9;-9;-1;-9;-1;-9;-1;-9;-9;-9;-9;-1;-9;-9;-1;-1;-9;-1;-1;0000;FFF8;-9;-9;-9;-1;-9;-1;-9;FFF6;-9;-1;-9;-1;-9;-1;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9;-9');

SELECT
    i,
    val1=n.v.value('/e[1]','VARCHAR(16)'),
    val2=n.v.value('/e[2]','VARCHAR(16)'),
    val3=n.v.value('/e[3]','VARCHAR(16)'),
    -- ... repeat for val4 .. val114
    val115=n.v.value('/e[115]','VARCHAR(16)')
FROM
    @tt
    CROSS APPLY (
        SELECT 
            CAST('<e>'+REPLACE(x,';','</e><e>')+'</e>' AS XML) AS itm
    ) AS i
    CROSS APPLY i.itm.nodes('/') AS n(v);
Comment

PREVIOUS NEXT
Code Example
Python :: python file 
Python :: add items to list python 
Python :: python list copy 
Python :: matplotlib units of scatter size 
Python :: python copy vs deepcopy 
Python :: how to change help command on discord python 
Python :: run python code online 
Python :: create a new column in pandas dataframe based on the existing columns 
Python :: subarrays in python 
Python :: iloc[:,0:-1] 
Python :: python package install 
Python :: looping nested dictionaries 
Python :: matrix multiplication python without numpy 
Python :: python and flask create_app 
Python :: python use cases 
Python :: python function arguments 
Python :: __call__() python 
Python :: add one element to tuple python 
Python :: search object in array python 
Python :: add python to path windows 10 
Python :: what is an indefinite loop 
Python :: isenable selenium python 
Python :: convert string input into a nested tuple in python 
Python :: eastvale roblox python 
Python :: python - dashboard 
Python :: pandas read sql generator to dataframe 
Python :: restart device micropython 
Python :: how to find 6,6,77,8 in python 
Python :: loosen_pickle 
Python :: how to app object pyhthon 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =