Pandas format column as currency

前端 未结 2 1212
温柔的废话
温柔的废话 2021-02-20 10:41

Given the following data frame:

import pandas as pd
df = pd.DataFrame(
        {\'A\':[\'A\',\'B\',\'C\',\'D\'],
         \'C\':[12355.00,12555.67,640.00,7000]
          


        
相关标签:
2条回答
  • 2021-02-20 11:10
        def format(x):
            return "${:.1f}K".format(x/1000)
    
    
        df = pd.DataFrame(
            {'A':['A','B','C','D'],
             'C':[12355.00,12555.67,640.00,7000]
            })
    
        df['C'] = df['C'].apply(format)
    
        print(df)
    
    0 讨论(0)
  • 2021-02-20 11:26

    Or you could use a lambda function for shorter syntax

    df['C'] = df['C'].apply(lambda x: "${:.1f}k".format((x/1000)))
    
    0 讨论(0)
提交回复
热议问题