2017-04-13 81 views
3

鑑於這個數據幀:熊貓數據幀部分字符串替換

import pandas as pd 
d=pd.DataFrame({'A':['a','b',99],'B':[1,2,'99'],'C':['abcd99',4,5]}) 
d 

    A B C 
0 a 1 abcd* 
1 b 2 4 
2 99 99 5 

我想要替換所有99S中有星號的整個數據幀。 我已經試過這樣:

d.replace('99','*') 

...但它只是在串99的情況下,事先曾在B列

謝謝!

回答

2

使用numpy的性格功能

d.values[:] = np.core.defchararray.replace(d.values.astype(str), '99', '*') 
d 

    A B  C 
0 a 1 abcd* 
1 b 2  4 
2 * *  5 

天真的時間測試

enter image description here

2

問題是在列A值99和B是不同的類型:

>>> type(d.loc[2,"A"]) 
<class 'int'> 
>>> type(d.loc[2,"B"]) 
<class 'str'> 

您可以通過df.astype()投你數據幀爲字符串類型,然後替換,導致:

>>> d.astype(str).replace("99","*") 
    A B  C 
0 a 1 abcd99 
1 b 2  4 
2 * *  5 

編輯:使用正則表達式是其他答案給出的正確解決方案。由於某種原因,我錯過了DataFrame中的abcd *。

這裏會留下這個,以防萬一它對別人有幫助。

3

如果要更換所有的99 S,嘗試使用正則表達式

>>> d.astype(str).replace('99','*',regex=True)

A B C 
0 a 1 abcd* 
1 b 2 4 
2 * * 5 
3

這將做的工作:

import pandas as pd 
d=pd.DataFrame({'A':['a','b',99],'B':[1,2,'99'],'C':['abcd99',4,5]}) 
d=d.astype(str) 
d.replace('99','*',regex=True) 

這給

A B C 
0 a 1 abcd* 
1 b 2 4 
2 * * 5 

請注意,這會創建一個新的數據框。你也可以做到這一點,而不是就地:

d.replace('99','*',regex=True,inplace=True)