2017-08-02 86 views
1

我正在從一個數據框中刪除行,該數據框恰好與特定列下的$1匹配。

我這樣做與

apts = apts[~apts.Price.str.contains('$1')] 

這不會刪除任何行。但我知道我在價格欄下有$1

例如,返回true:

if str(apts.ix[8193]['Price']) == '$1': 
    print('True') 

任何想法什麼回事?

+0

有關說明,如果您提供了幾行數據幀,可能會有所幫助。 – mmenschig

回答

0

它看起來像熊貓contains方法,除了一個正則表達式,在這種情況下,$用於表示行結束。你可能想用\逃避你$,所以你的代碼將apts = apts[~apts.Price.str.contains('\$1')]

1

$是一個具有特殊意義的元字符。您需要將其轉義以實際匹配。從here你可以看到它依賴於正則表達式。

apts = apts[~apts.Price.str.contains('\$1')] 
0

除了逃避'$''\$',它知道你可以把正則表達式使用了與regex=False

考慮當時使用regex=False

樣本數據幀 apts

apts = pd.DataFrame(dict(Price=['2,000', '$1,000', '1000', '$14'])) 

    Price 
0 2,000 
1 $1,000 
2 1000 
3  $14 

很重要

apts[apts.Price.str.contains('$', regex=False)] 

    Price 
0 2,000 
2 1000