在小数点前后填充零。

huangapple go评论69阅读模式
英文:

padding zeros before and after the decimal point

问题

我正在使用Python 3。

我有一个名为“data”的数据帧中的一个名为“CIPCODE”的十进制数字列。该列的整数部分范围从1到60。

我想要格式化它,使之满足以下条件:

第一个条件是: 如果整数部分的值介于1到9之间(包括1和9),则在数字前面加一个零,例如 -

4.2021 变为 04.2021

25.3434 保持不变

所以基本上,我们应该始终在小数点前有2位数字。

第二个条件是: 小数点后始终应该有4位数字,例如 -

51.201 变为 51.2010

34.5555 保持不变

我尝试过以下方法:

data['CIPCODE'] = data['CIPCODE'].astype(str).str.zfill(7)

但这只会在小数点前面填充零。

英文:

I am using python 3.

I have a column of decimal numbers called “CIPCODE” in a dataframe called “data”. The integer part of this column ranges from 1 to 60.

I want to format it such that:

the first condition is: if the value of the integer part is between 1 and 9 (inclusive), then add a zero in front of the number, for example -

4.2021 becomes 04.2021

25.3434 remains 25.3434

so basically, we should always have 2 digits before the decimal.

the second condition is: that there should always be 4 digits after the decimal, for example -

51.201 becomes 51.2010

34.5555 remains 34.5555

I have tried the following:

data['CIPCODE'] = data['CIPCODE'].astype(str).str.zfill(7)

but this only pads zeros to the part before the decimal point.

答案1

得分: 4

你可以直接使用Python格式化(07.4f,表示小数点后4位,总共填充为7个字符):

df['formatted'] = df['CIPCODE'].apply(lambda x: f'{x:07.4f}')

输出:

   CIPCODE formatted
0   4.2021   04.2021
1  25.3434   25.3434
2  12.3000   12.3000
英文:

You can directly use python formatting (07.4f, meaning 4 digits after the decimal and padded to 7 characters) :

df['formatted'] = df['CIPCODE'].apply(lambda x: f'{x:07.4f}')

Output:

   CIPCODE formatted
0   4.2021   04.2021
1  25.3434   25.3434
2  12.3000   12.3000

答案2

得分: 2

使用 python格式化 来自定义字符串:

data = pd.DataFrame({'CIPCODE':[4.2021,25.3434,51.201,34.5555]})

data['CIPCODE'] = data['CIPCODE'].apply('{:07.4f}'.format)

print (data)
   CIPCODE
0  04.2021
1  25.3434
2  51.2010
3  34.5555
      {:07.4f}
        ↑ ↑ 
        | |
# 要填充的数字 | | # 要显示的小数位数
英文:

Use python formating for custom string:

data = pd.DataFrame({'CIPCODE':[4.2021,25.3434,51.201,34.5555]})

data['CIPCODE'] = data['CIPCODE'].apply('{:07.4f}'.format)

print (data)
   CIPCODE
0  04.2021
1  25.3434
2  51.2010
3  34.5555

              {:07.4f}
                ↑ ↑ 
                | |
# digits to pad | | # of decimal places to display

答案3

得分: 1

这可能不是最快的方法,但你可以通过将这个逻辑放入一个函数中,然后应用到数据框上来实现。

示例:

import pandas as pd
from collections import deque

df = pd.DataFrame({'CIPCODE': [4.2021, 25.3434, 51.201, 34.5555]}) 

def format_cipcode(code):
    d = deque(code)
    if d[1] == '.':
        d.appendleft('0')
    if d[-4] == '.':
        d.append('0')
    return ''.join(d)

df['CIPCODE'] = df['CIPCODE'].astype(str).apply(format_cipcode)

print(df['CIPCODE'])

输出:

0    04.2021
1    25.3434
2    51.2010
3    34.5555
Name: CIPCODE, dtype: object

在这个例子中,我使用了一个双向队列(deque),它是一种可以在左侧和右侧追加元素的列表。对于左侧,检查字符串中的第二个字符是否为小数点(.),如果是,则在左侧追加一个0。对于右侧,检查字符串倒数第四个字符是否为小数点(.),如果是,则追加一个0。

英文:

Might not be the fastest way, but you can do this by putting this logic into a function and applying it to the dataframe.

Example:

import pandas as pd
from collections import deque

df = pd.DataFrame({'CIPCODE': [4.2021, 25.3434, 51.201, 34.5555]}) 

def format_cipcode(code):
    d = deque(code)
    if d[1] == '.':
        d.appendleft('0')
    if d[-4] == '.':
        d.append('0')
    return ''.join(d)


df['CIPCODE'] = df['CIPCODE'].astype(str).apply(format_cipcode)

print(df['CIPCODE'])

Output:

Name: CIPCODE, dtype: object
0    04.2021
1    25.3434
2    51.2010
3    34.5555

In this case I used a deque, which is a sort of list that you can append to left and right. For the left side, check if the second character in the string is a . and append a 0 if it is. For the right side, check if the fourth character from the right is a . and if so, append a 0

huangapple
  • 本文由 发表于 2023年6月1日 14:49:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定