[英]How to Remove particular changing text in from a DataFrame column in Python?
[英]How to remove text and remain with interger values in python dataframe column
我有一個包含該列的數據框,如下所示;
ID, Quantity
1 1,000 total
2 802 destroyed
3 >689 total
4 1,234-1,900 lost
我希望輸出如下:
ID, Quantity
1 1,000
2 802
3 689
4 1234-1,900
我試過了,
df['Quantity'] = df['Quantity'].str.replace(r' \s', '')
至今沒有成功。
這取決於數量列中的可能值。 如果數字部分永遠不能有空格(如您的示例),您可以使用Series.str.partition
:
number_column, space_column, text_column = df['Quantity'].str.partition()
del space_column # These two lines are not required but I like to include them
del text_column # to improve code readability and keep pylint happy
df['Quatity'] = number_column
這也可以寫成一行:
df['Quantity'] = df['Quantity'].str.partition()[0]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.