2016-02-25 79 views
0

我有一個數據幀,其中features -column的值是字典般如下:字典的重映射值快譯通到列大熊貓

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

回答

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

如何我'NaN'和'{}'值奮鬥?如果isinstance(x,dict)和x],index = df.index)引發錯誤* ValueError:傳遞值的形狀是(17, 66),指數意味着(17,105)* – SpanishBoy

+0

是的,這是問題。一種解決方法是將'NaN'替換爲'{}' - [see](http://stackoverflow.com/a/34991815/2901002) – jezrael