感謝您訪問我的文章。下面我有以下數據框:如果它們出現在列表中,則替換整個數據框中的字符串
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")