2016-02-26 73 views
1

在這裏是一個示例大熊貓數據幀:系列對象沒有屬性「條」

id product_type qty 
1 product_type 1 100 
2 product_type 2 300 
3 product_type 1 200 

我想以此獲得了以下新的數據幀,以刪除product_type在列product_type

id product_type qty 
1 1    100 
2 2    300 
3 1    200 

這是我如何嘗試去做的:

orders['product_type'].strip('product_type ') 

但是有一個錯誤:

'Series' object has no attribute 'strip' 

回答

1

你需要在它前面.str因爲它是一個string accessor method

orders['product_type'].str.strip('product_type ') 



In [6]: 
df['product_type'] = df['product_type'].str.strip('product_type ') 
df 

Out[6]: 
    id product_type qty 
0 1   1 100 
1 2   2 300 
2 3   1 200 

,或通過正則表達式來提取號碼str.extract

In [8]: 
df['product_type'] = df['product_type'].str.extract(r'(\d+)') 
df 

Out[8]: 
    id product_type qty 
0 1   1 100 
1 2   2 300 
2 3   1 200 
+0

會自動更新數據幀或我應該將這個表達式的結果保存在一個新的數據框中?即訂單=訂單[...] ... – JoeBlack

+0

是的,你需要分配結果返回 – EdChum