Pandas DataFrame将字符串拆分为带有条件和缺少数据的多列

时间:2018-11-13 14:56:42

标签: python string pandas dataframe

所以我有一个看起来像这样的DataFrame:

df = pd.DataFrame({'feature1':[34,45,52],'feature2':[1,0,1],'unparsed_features':["neoclassical, heavy, $2, old, bronze", "romanticism, gold, $5", "baroque, xs, $3, new"]})

df
       feature1  feature2                     unparsed_features
    0        34         1  neoclassical, heavy, $2, old, bronze
    1        45         0                 romanticism, gold, $5
    2        52         1                  baroque, xs, $3, new

我正在尝试将unparsed_features列分为6列(重量,年龄,颜色,大小,价格和期间),但是您可以看到订单混乱,不仅如此,某些字段丢失了

我对每一列的大概含义如下所示:

main_dict = {
 'weight': ['heavy','light'],
 'age': ['new','old'],
 'colour': ['gold','silver','bronze'],
 'size': ['xs','s','m','l','xl','xxl','xxxl'],
 'price': ['$'],
 'period': ['renaissance','baroque','rococo','neoclassical','romanticism']
}

理想情况下,我希望我的数据框如下所示:

df
   feature1  feature2                     unparsed_features weight price  age  \
0        34         1  neoclassical, heavy, $2, old, bronze  heavy    $2  old   
1        45         0                 romanticism, gold, $5           $5        
2        52         1                  baroque, xs, $3, new           $3  new   

  size  colour        period  
0       bronze  neoclassical  
1         gold   romanticism  
2   xs               baroque

我知道第一步是用逗号分割字符串,但此后我迷路了。

df['unparsed_features'].str.split(',')

谢谢您的帮助。

2 个答案:

答案 0 :(得分:0)

由于'unparsed_features'中的数据在每一行中都不具有相同的结构,因此不确定是否有简便的方法。一种方法是使用您定义的字典main_dict,遍历每个项目,并将str.extract与参数pat一起使用,与price有点不同:

for key, list_item in main_dict.items():
    if key =='price':
        df[key] = df.unparsed_features.str.extract('(\$\d+)').fillna('')
    else:
        df[key] = df.unparsed_features.str.extract('((^|\W)' +'|(^|\W)'.join(list_item) + ')').fillna('')

\$\d+允许在符号$之后寻找任何数字,而(^|\W)可以在list_item中的任何单词之前寻找空格或行首。 >

您会得到预期的结果:

   feature1  feature2                     unparsed_features  weight   age  \
0        34         1  neoclassical, heavy, $2, old, bronze   heavy   old   
1        45         0                 romanticism, gold, $5                 
2        52         1                  baroque, xs, $3, new           new   

    colour size price        period  
0   bronze         $2  neoclassical  
1     gold         $5   romanticism  
2            xs    $3       baroque  

答案 1 :(得分:0)

坦白说,W-B是正确的,您需要修改您的字典,但是要解决下面的可用数据是我的方法

for keys in main_dict:
    data_list = []
    for value in df.unparsed_features: # for every row
        for l_data in main_dict[keys]:
            if keys == 'price':
                matching = [v for v in value.split(',') if l_data in v]
            else:
                matching = [v for v in value.split(',') if l_data == v.strip()]

            if matching:
                break

        if matching:
            data_list.append(matching[0])
        else:
            data_list.append(None)

        matching = ''  
    df[keys] = data_list

输出

   feature1  feature2                     unparsed_features  weight   age  \
0        34         1  neoclassical, heavy, $2, old, bronze   heavy   old   
1        45         0                 romanticism, gold, $5    None  None   
2        52         1                  baroque, xs, $3, new    None   new   

    colour  size price        period  
0   bronze  None    $2  neoclassical  
1     gold  None    $5   romanticism  
2     None    xs    $3       baroque