Q: Fast way for partitioning a data frame column I have a data frame of nearly 400,000 rows. One column contains codes like R5678.a_D697_E5879. I want the parts before the first . and _ stored in two separate columns. I wrote some code to achieve this, but it is terribly slow. Below you can find an example. Is there a way for speeding up the code?
import pandas as pd
pd.options.mode.chained_assignment = None

data = {'name':['tom', 'nick', 'krish', 'jack'],
        'code':['R5678.a_D697_E5879', 'R5008.f_H697_E5800', \
                'R00778.b_P6_E000', 'L001.a_J847_E5111']}

# create DataFrame
df = pd.DataFrame(data)

# create columns in which to store results
df['lp_code'] = ''
df['lp_code_reduc'] = ''

for index in range(df.shape[0]):
    
    # store part before first '_'
    df.lp_code.iloc[index] = df.code.iloc[index].partition('_')[0]
    # store part before '.'
    df.lp_code_reduc.iloc[index] = df.lp_code.iloc[index][:-2]


A: Try via str.split():
df['lp_code']=p_code=df['code'].str.split('_',1).str[0]
df['lp_code_reduc']=df['lp_code'].str.split('.').str[0]

OR
via assign() and str.split():
df=df.assign(lp_code=df['code'].str.split('_',1).str[0],lp_code_reduc=df['lp_code'].str.split('.').str[0])

