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

时间:2018-10-14 13:07:36

标签: python regex pandas string-formatting

我有下面的例子

    chartView.resize(this->size())

所需的输出:

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 ,但不确定如何应用。

3 个答案:

答案 0 :(得分:2)

这可能有用:

if len("%0.f" % floating.number) >= 5:
    print ('do something') 

答案 1 :(得分:1)

这应该有效。

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)

答案 2 :(得分:1)

尝试一下

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)