首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Excel表格中使用re库或其他方法将字符串拆分为文本和数字?

如何在Excel表格中使用re库或其他方法将字符串拆分为文本和数字?
EN

Stack Overflow用户
提问于 2020-08-18 06:40:25
回答 2查看 105关注 0票数 0

我需要将Excel工作表的第一列转换为整数值。需要删除字符串(比如LP001005,删除LP并获取数字的其余部分)。

我能够在单个变量上实现这一点。但是,我需要在Excel工作表上实现这一点。我的意思是将整个Excel转换为LP001005中的数据帧,提取Loan_ID并进行转换(从pandas中删除LP ),然后使用数据帧。

代码语言:javascript
复制
>>> import re
>>> test_str = "Geeks4321"
>>> print("The original string is : " + str(test_str))
The original string is : Geeks4321
>>> res = [re.findall(r'(\d+)', test_str)[0] ]
>>> print("The tuple after the split of string and number : " + str(res))
The tuple after the split of string and number : ['4321']
>>>

Excel工作表如下所示:

代码语言:javascript
复制
LoanID Name
LP1401 Shubhra
LP1102 Ankit
LP1203 Sowmya
EN

回答 2

Stack Overflow用户

发布于 2020-08-18 07:06:45

您可以使用.extract()方法提取贷款ID的数字部分:

代码语言:javascript
复制
df = pd.DataFrame({'LoanID': 'LP1401 LP2102 LP3203'.split(),
                  'Name': 'Shubhra Ankit Sowmya'.split()})

df['LoanID'] = df['LoanID'].str.extract( r'\w(\d+)', expand=False ).astype(int)

print(df)

   LoanID    Name
0    1401  Shubhra
1    2102    Ankit
2    3203   Sowmya
票数 0
EN

Stack Overflow用户

发布于 2020-08-18 08:42:53

在Jupyter上试试这个:

代码语言:javascript
复制
import pandas as pd
# open your excel file with pandas with the (read_excel) method:
f= pd.read_excel('Book1.xlsx',sheet_name='Sheet1')
# you may check the content of the first column:
for i in f.iloc[:,0]:
    print(i)
# check the headers names as objects:
f.columns.ravel()
# finally extract the numbers from the first column:
f['LoanID '].str.extract('(\d+)')
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63459550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档