2017-04-13 19 views
2

感謝您訪問我的文章。下面我有以下數據框:如果它們出現在列表中,則替換整個數據框中的字符串

df1 
     col1         col2 
    1 virginia is cold, canada is cold too virginia is cold, canada is cold too 
    2 florida, virginia, washington are good florida, virginia, washington are good 
    3 georgia, alabama, virginia are hot  virginia is cold, canada is cold too 
    4 virginia, ohio, new castle are great hawaii, nebreska is wonderful 
    5 hawaii, nebreska is wonderful   virginia, ohio, new castle are great 

另外,我有一個包含字符串列表:

lst = ['virginia', 'hot', 'too'] 

我想用「XXXXXX」替換字符串中的整個數據幀,如果它的比賽之一列表中的字符串。例如,我的數據幀是這樣的置換後:

df1 
      col1         col2 
     1 xxxxxx is cold, canada is cold xxxxxx xxxxxx is cold, canada is cold xxxxxx 
     2 florida, xxxxxx, washington are good florida, xxxxxx, washington are good 
     3 georgia, alabama, xxxxxx are xxxxxx  xxxxxx is cold, canada is cold xxxxxx 
     4 xxxxxx, ohio, new castle are great  hawaii, nebreska is wonderful 
     5 hawaii, nebreska is wonderful   xxxxxx, ohio, new castle are great 

到目前爲止,我已經試過,但它不工作:

df1 = df1.replace(lst, "xxxxxx") 

回答

3

您可以從單詞列表中創建詞典並使用正則表達式

lst = ['virginia', 'hot', 'too'] 
df1.replace({w: "xxxxxx" for w in lst}, regex=True) 

enter image description here

2

嘗試遍歷列表lst喜歡這裏:

import pandas as pd 

... 
lst = ['virginia', 'hot', 'too'] 
for s in lst: 
    df1.replace(s, 'xxxxx', inplace=True) 

print(df1) 
3
df1.replace(lst, 'x' * 5, regex=True) 

            col1         col2 
1 xxxxx is cold, canada is cold xxxxx xxxxx is cold, canada is cold xxxxx 
2 florida, xxxxx, washington are good florida, xxxxx, washington are good 
3 georgia, alabama, xxxxx are xxxxx xxxxx is cold, canada is cold xxxxx 
4 xxxxx, ohio, new castle are great  hawaii, nebreska is wonderful 
5  hawaii, nebreska is wonderful xxxxx, ohio, new castle are great 
相關問題