简体   繁体   English

将包含字符串内的列表的数据框列拆分为两列

[英]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. 我有一个df,其中一列,'vals'包含一个字符串内的列表。 I want to convert this into two columns 'val1' and 'vals2. 我想把它转换成两列'val1'和'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. 我试图拆分和剥离字符串,但无法找到实现为df中的每一行执行此操作。

    vals
'[12.1, 15.0]'

val1  val2
12.1  15.0

Use strip with split and casting to floats if necessary, last add prefix by add_prefix : 如有必要,使用strip split和转换的strip来浮动,最后通过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)

Here we employ ast.literal_eval - 在这里我们使用ast.literal_eval -

import ast
df = pd.DataFrame({'A':['[12.1, 15.0]','[12.4, 11.1]']})
df['val1']=df['A'].apply(lambda x:ast.literal_eval(x)[0])
df['val2']=df['A'].apply(lambda x:ast.literal_eval(x)[1])
df    
                A  val1  val2
  0  [12.1, 15.0]  12.1  15.0
  1  [12.4, 11.1]  12.4  11.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM