[英]How can I trim the string before first white space in a series Python text data?
在此數據中,我想修剪第一個空格之前是代碼的字符串。 並且還想刪除python中“ IN”字符后的字符串。 這里有一個簡單的例子。 我在像這樣的大熊貓中有專欄。
line = ["RCI-AMG-3664 Laboratory Specialist","RCI-VBC-09568 Administrative Assistant in Cary, NC"]
我假設您正在逐行閱讀文本並將每一行保存為字符串? 如果您在字符串“ a”中保存了一行,
a[a.find(' ')+1:]
將僅返回第一個空格之后的字符串部分。
編輯用戶澄清了這些行是熊貓系列的。 在這種情況下,上面的代碼仍然有效,但是我們需要將其應用到該系列中:
string_formatter = lambda a: a[a.find(' ')+1:]
your_series = your_series.apply(string_formatter)
//this will cause the series to cut each string up to the first whitespace
如果系列可以包含字符串以外的對象,則需要在函數中添加類型檢查。
import re
def remove_noise(data):
ans = re.sub('\s+',' ',data)
ans = re.sub('\sin\s.*','',data)
ans = data.split(' ')
del(data[0])
ans = ' '.join(map(str, data))
return ans;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.