【问题标题】:Remap values-dict to columns in Pandas将 values-dict 重新映射到 Pandas 中的列
【发布时间】:2016-06-08 07:21:12
【问题描述】:

我有一个数据框,其中 features-column 的值类似于 dict,如下所示:

http://screencast.com/t/0Ko0NIBLwo

   features                    name             price  rating  read reviews
9  {'Cooking...': '- S...', }  Master Chef...  $279.99   None  None      {}  

字典示例:

{u'Cooking Type': u'- Specialty Cooking', u'Cooking Area': u'- Backyard', u'Brand Name': u'- Pizzacraft', u'Fuel Type': u'- Propane', u'Product Type': u'- BBQ', u'Size': u'- Medium Size'}

是否可以将这些值转换为新列?

   features                    Cooking Type       Specialty Cooking  ... name             price  rating  read reviews
9  {'Cooking...': '- S...', }  Specialty Cooking   Backyard          ... Master Chef...    $279.99   None  None      {}  

【问题讨论】:

    标签: python python-2.7 dictionary pandas


    【解决方案1】:

    我认为你可以使用replacestripconcat

    print df
                                                features          name    price  \
    0  {u'Cooking Type': u'- Specialty Cooking', u'Co...  Master Chef1  $279.99   
    1  {u'Cooking Type': u'- Specialty Cooking', u'Co...  Master Chef3  $279.99   
    
      rating  read reviews  
    0   None  None      {}  
    1   None  None      {}  
    
    df1 = pd.DataFrame([x for x in df['features']], index=df.index)
    
    for col in df1.columns:
        df1[col] = df1[col].str.replace(r'-','').str.strip()
    
    print df1
       Brand Name Cooking Area       Cooking Type Fuel Type Product Type  \
    0  Pizzacraft     Backyard  Specialty Cooking   Propane          BBQ   
    1  Pizzacraft     Backyard  Specialty Cooking   Propane          BBQ   
    
              Size  
    0  Medium Size  
    1  Medium Size  
    
    df = pd.concat([df1, df[['name','price','rating','read','reviews']]], axis=1)
    print df
       Brand Name Cooking Area       Cooking Type Fuel Type Product Type  \
    0  Pizzacraft     Backyard  Specialty Cooking   Propane          BBQ   
    1  Pizzacraft     Backyard  Specialty Cooking   Propane          BBQ   
    
              Size          name    price rating  read reviews  
    0  Medium Size  Master Chef1  $279.99   None  None      {}  
    1  Medium Size  Master Chef3  $279.99   None  None      {}  
    

    【讨论】:

    • 我该如何处理NaN{} 的价值观? df1 = pd.DataFrame([x for x in df['FEATURES'] if isinstance(x, dict) and x], index=df.index) 引发错误 ValueError: 传递值的形状为 (17, 66),索引暗示 (17, 105)
    • 是的,这是个问题。一种解决方案是将NaN 替换为{} - see
    猜你喜欢
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    相关资源
    最近更新 更多