简体   繁体   English

如何更改pandas列中数字的格式?

[英]How to change the format of a number in a pandas column?

I have a large DataFrame of numbers but each individual number follows a different format. 我有一个很大的数字DataFrame,但是每个数字都遵循不同的格式。 I want to use a regular expression to replace a large amount of them with a 111-111-1111 format 我想使用正则表达式以111-111-1111格式替换大量的正则表达式

numbers["numbers"].replace('^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$, "/*/*/*-/*/*/*-/*/*/*/*", regex=True')

it should take a number found by the expression and keep the base number but change its format. 它应该采用表达式找到的数字,并保留基数,但更改其格式。 1234567890 should equal 123-456-7890 1234567890应该等于123-456-7890

You may use 您可以使用

df["numbers"] = df["numbers"].str.replace('^(?:\+\d{1,2}\s)?\(?(\d{3})\)?[\s.-]?(\d{3})[\s.-]?(\d{4})$', r'\1-\2-\3')

Details 细节

  • ^ - start of string ^ -字符串开头
  • (?:\\+\\d{1,2}\\s)? - an optional sequence of -的可选顺序
  • \\(? - an optional ( \\(? -可选(
  • (\\d{3}) - Group 1: three digits (\\d{3}) -第1组:三位数
  • \\)? - an optional ) -可选)
  • [\\s.-]? - an optional whitespace, . -可选的空格. or - -
  • (\\d{3}) - Group 2: three digits (\\d{3}) -第2组:三位数
  • [\\s.-]? - an optional whitespace, . -可选的空格. or - -
  • (\\d{4}) - Group 3: four digits (\\d{4}) -第3组:四位数
  • $ - end of string. $ -字符串结尾。

The \\x in the replacement pattern ( r'\\1-\\2-\\3' ) are placeholders for the values captured with corresponding groups. 替换模式( r'\\1-\\2-\\3' )中的\\x是用相应组捕获的值的占位符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM