iT邦幫忙

0

Pandas內容篩選取代

  • 分享至 

  • twitterImage
import pandas as pd
df = pd.DataFrame({
                    'A': ['one', 'two', 'three','four'],
                    'B': ['TCP/56617', 'TCP/56575', 'http', 'https']
                })

想請問各位大大
我想要將'TCP/56617'、'TCP/56575'全部用'TCP'這個字串取代
該怎麼做呢。
查了半天只會字串一對一的取代,麻煩各位了

看更多先前的討論...收起先前的討論...
ccutmis iT邦高手 2 級 ‧ 2020-04-15 09:09:19 檢舉
#用regex搜出匹配'開頭是TCP/結尾是5個數字'的字串並用'TCP'取代
df.replace(to_replace=r'^TCP\/\d{5}$', value='TCP', regex=True)

參考: google 'pandas find and replace cell value'
draguitar iT邦新手 4 級 ‧ 2020-04-15 11:06:15 檢舉
想請問,若是TCP開頭的都取代成TCP
df.replace(to_replace=r'^TCP', value='TCP', regex=True)
這樣寫可以嗎
謝謝了><
^TCP.*
draguitar iT邦新手 4 級 ‧ 2020-04-15 11:36:29 檢舉
非常感謝!!
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答