python - 获取 10 位数字

标签 python pandas dataframe

我正在尝试定义一个函数,该函数将创建一个列并将数字清理为仅十位数的区号和号码。日期框架。

         PNum1     
0      18888888888
1       1999999999
2  +++(112)31243134 

我拥有所有单独的函数,甚至将它们存储到数据帧和字典中。

def GetGoodNumbers(col):
    column = col.copy()
    Cleaned = column.replace('\D+', '', regex=True)
    NumberCount = Cleaned.astype(str).str.len()
    FirstNumber = Cleaned.astype(str).str[0]
    SummaryNum = {'Number':Cleaned,'First':FirstNumber,'Count':NumberCount}
    df = pd.DataFrame(data=SummaryNum)
    DecentNumbers = [] 
    return df

返回

   Count First        Number
0     11     1   18888888888
1     10     3    3999999999
2     11     2   11231243134

如何循环遍历数据框列并返回一个新列,该新列将: - 删除所有非数字。 -获取长度(通常为 10 或 11) -如果长度为11,则返回右10位。

所需的输出:

number
1231243134
1999999999
8888888888

最佳答案

您可以删除所有非数字并分割最后 10 位数字。

df.PNum1.str.replace('\D+', '').str[-10:]

0    8888888888
1    1999999999
2    1231243134
Name: PNum1, dtype: object

关于python - 获取 10 位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48452090/

相关文章:

python - Python的层次结构遍历和比较模块?

python - Pandas:将 DataFrame 转换为每个单元格的均值和标准差

python - 通过在 Python 中循环行来连接

python:循环遍历Excel中的索引并用字符串替换

python - 如何在 python 中添加/替换文件扩展名?

python - 更改 Django/Postgres db_table 名称

python - 在带有 unittest 的自动 Selenium 测试中使用多个 WebSocket 客户端

python - pandas 清理数据框

python - 如何计算DataFrame中字符串中的单词数?

python - 从列中提取部分值