如何删除列中的int。 Python熊猫

问题描述 投票:0回答:1

我有这样的df,如何删除列中的所有Int?

例如,列[0] ['material']的值从'lm792'转换为'lm'。

      material  item
index       
0   lm792         1
1   sotl085-pu01. 1
2   lm792         1
3   sotl085-pu01. 1
4   ym11-3527     1
... ... ...
135526  0         0
135527  0         0
135528  0         0
135529  0         0
135530  0         0
python pandas
1个回答
0
投票

您可以使用简单的正则表达式-

\d是数字(0-9范围内的字符),+表示1次或多次。因此,\d+是1个或多个数字。

df['material'] = df['material'].str.replace('\d+','')

print(df)
material  item
0        lm   1.0
1  sotl-pu.   1.0
2        lm   1.0
3  sotl-pu.   1.0
4       ym-   1.0
5             NaN
6             NaN
7             NaN
8             NaN
9             0.0
© www.soinside.com 2019 - 2024. All rights reserved.