将值-dict重新映射到Pandas中的列

时间:2016-02-25 13:27:32

标签: python python-2.7 dictionary pandas

我有一个数据框,其中features - 列的值与dict类似:

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

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

dict的例子:

{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      {}  

1 个答案:

答案 0 :(得分:1)

我认为您可以使用replacestrip以及concat

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      {}