首页 > 解决方案 > 我必须删除 Python 数据框中列中的最后一个数字和字符?

问题描述

我有一列具有这样的值:34343434,4223232,5.

我想删除最后一个数字和字符,,并期待这样:34343434 223232

我尝试使用此代码:

dataframe['column name'] = pd.to_numeric(dataframe['column name']).astype.float().round(0, 2)

但它没有用。有人可以帮我得到我想要的输出吗?

标签: python-2.7

解决方案


您可以使用正则表达式尝试这种方式:

dataframe['colname'] = dataframe['colname'].str.replace(',.','',regex=True)

文档链接


推荐阅读