0
我有這樣如何分割數字和文字
data = pd.DataFrame({ 'a' : [5, 5, '2 bad']})
我想將其轉換爲
{ 'a.digits' : [5, 5, 2], 'a.text' : [nan, nan, 'bad']}
我可以得到一個數據集「a.digits」爲波紋管
data['a.digits'] = data['a'].replace('[^0-9]', '', regex = True)
5 2
2 1
Name: a, dtype: int64
當我做
data['a'] = data['a'].replace('[^\D]', '', regex = True)
或
data['a'] = data['a'].replace('[^a-zA-Z]', '', regex = True)
我得到
5 2
bad 1
Name: a, dtype: int64
有什麼不對?如何刪除數字?
的replace()方法僅適用於字符串,但它確實對整數什麼。這就是爲什麼你的解決方案不起作用。 –
您可以使用循環來檢查每個元素的類型,如果它是整數(或只保留字符串),則跳過它。 –