首页 > 解决方案 > Split dataframe column containing list inside a string to two columns

问题描述

I have a df where one column, 'vals' contains a list inside a string. I want to convert this into two columns 'val1' and 'vals2. I have tried to split and strip the string but can't find an implementation to do this for every row in the df.

    vals
'[12.1, 15.0]'

val1  val2
12.1  15.0

标签: pythonstringpandassplit

解决方案


Use strip with split and casting to floats if necessary, last add prefix by add_prefix:

df = pd.DataFrame({'vals':["'[12.1, 15.0]'","'[12.1, 15.0]'"]})

df = (df['vals'].str.strip("'[]")
               .str.split(', ', expand=True)
               .astype(float)
               .add_prefix('val'))

If no missing values and performance is important:

df =  pd.DataFrame([x.strip("'[]").split(', ') for x in df['vals']], 
                    columns = ['val1', 'val2']).astype(float)

推荐阅读