Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在dataframe中查找字符串中的数字,并使用千位分隔符重新格式化该数字?_Python_Regex_Pandas_String Formatting - Fatal编程技术网

Python 如何在dataframe中查找字符串中的数字,并使用千位分隔符重新格式化该数字?

Python 如何在dataframe中查找字符串中的数字,并使用千位分隔符重新格式化该数字?,python,regex,pandas,string-formatting,Python,Regex,Pandas,String Formatting,我有下面的例子 df = pd.DataFrame({'City': ['Houston', 'Austin', 'Hoover','NY','LA'], 'Rules': ['ACH_CM > 28419581.51 and AMT_PM > 30572998.00 and AMT_PPM > 30572998.00 and AMT_CM > 30572998.00' , '

我有下面的例子

df = pd.DataFrame({'City': ['Houston', 'Austin', 'Hoover','NY','LA'],
                   'Rules': ['ACH_CM > 28419581.51 and AMT_PM > 30572998.00 and AMT_PPM > 30572998.00 and AMT_CM > 30572998.00'
                             , 'MAA_PM and _AMT_PPM > 30572998.00 and _AMT_PM > 16484703.01 and AMT_CM > 28419581.51'
                             , 'MAA_PM and AMT_CM > 284 and AMT_PM > 30572998.00 and AMT_PPM > 30572998.00 and AMT_PPPM > 30572998.00 and ACH_AMT_PPM > 16484703.01'
                            ,'MAA_CM'
                            ,'_AMT_PPM > 30572.00']},columns=['City', 'Rules'])
期望输出:

City    Rules
Houston ACH_CM > 28,419,581.51 and AMT_PM > 30,572,998.00 and AMT_PPM > 30,572,998.00 and AMT_CM > 30,572,998.00
Austin  MAA_PM and _AMT_PPM > 30,572,998.00 and _AMT_PM > 16,484,703.01 and AMT_CM > 28,419,581.51
Hoover  MAA_PM and AMT_CM > 284 and AMT_PM > 30,572,998.00 and AMT_PPM > 30,572,998.00 and AMT_PPPM > 30,572,998.00 and ACH_AMT_PPM > 16,484,703.01
NY      MAA_CM
LA      AMT_PPM > 30,572.00
我认为我应该使用
“{0:,.0f}”。格式
,但不确定如何应用它。

这可能有用:

if len("%0.f" % floating.number) >= 5:
    print ('do something') 
这应该行得通

def _format(x):
    unformatted = re.findall("\d+\.\d+", df['Rules'].iloc[0])
    formatted = ['{:,}'.format(float(x)) for x in unformatted]
    for i in range(len(unformatted)):
        x = x.replace(unformatted[i], formatted[i])
    return x

df['Rules'] = df['Rules'].map(_format)
试试这个

df['Rules'] = df.Rules.apply(lambda x: re.sub("\d+\.\d+", my_func, x))
其中
my_func
定义如下:

def my_func(matchobj):
    f = float(matchobj.group(0))
    return "{0:,.2f}".format(f)

谢谢,但是如果任何给定的行有超过1个浮点数,这将不起作用,例如,在第二行中,这不是拾取此_AMT_PM>16484703.01并将其转换为千分浮点数。我编辑了该示例以添加不包含任何数字的行。我猜您的目的是将逗号作为千分分隔符。所以如果你有2345号,你会期待2345号。谢谢你,这几乎就是我需要的!为了适应和概括我当前的问题,我需要做的唯一修改是将正则表达式修改为:['\d{4,}.\d+']对不起,下面是格式正确的注释:谢谢,这几乎就是我需要的!为了适应和概括我当前的问题,我需要做的唯一修改是将正则表达式修改为:
\d{4,}.\d+
并在lambda中修改为
str(x)
@MartinPetrov,为什么需要修改正则表达式和
str(x)
?我之所以学习,是因为我只想捕获1000及以上的数字,而对于
str(x)
我将其应用于具有数千行的数据帧,如果我没有指定
str(x)
它会将x作为pd.Series读取,并抛出一个错误。