如何使用 Python 从 excel 中的列中获取子字符串?

我有一个 Excel 文件,我想读取该 Excel 文件中的特定列,我使用以下代码执行此操作:


import pandas as pd

import xlrd


file_location = input('Where is the file located? Please input the file path here. ')

column = input('In what column is the code? ')


code_array = pd.read_excel(file_location, usecols=column)

for i in code_array:

    print(code_array)

并且该代码在控制台中打印出该列的内容。现在,该列的文本如下:12345 - Description。我只想提取号码,我该怎么做?我想过使用 [0:5] 中的子字符串或将数据转换为字符串数组,但我不确定该怎么做。


慕村225694
浏览 166回答 1
1回答

慕码人8056858

如果数字每次都是 5 位长,您可以使用 lambda 快速创建一个子字符串。code_array["number_column"] = code_array["YourColumnNameHere"].apply(lambda x: str(x)[:5])如果每次的长度不一样,但是位置都一样,可以拆分成一个字符串数组,然后访问第一个元素:code_array["number_column"] = code_array["YourColumnNameHere"].apply(lambda x: str(x).split()[0])让我知道这是否解决了您的问题,否则我们将需要使用正则表达式。注意将 YourColumnNameHere 更改为与数据框中的列同名。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python