Add a percent sign to a dataframe column in Python

后端 未结 2 569
广开言路
广开言路 2020-12-18 02:21

I\'ve been attempting to add a percent sign to a column in my dataframe but to no avail. Would anyone have any idea?

import pandas as pd

names = (\'jimmy\',         


        
相关标签:
2条回答
  • 2020-12-18 02:50

    You can do it like that too :

    df['Percent'] = df['Grade'].apply( lambda x : str(x) + '%')
    
    0 讨论(0)
  • 2020-12-18 03:05

    Cast the dtype to str using astype:

    In [11]:
    df['Percent'] = df['Grade'].astype(str) + '%'
    df
    
    Out[11]:
       Grade     Name Percent
    0     82    jimmy     82%
    1     38      red     38%
    2     55    julie     55%
    3     19     brad     19%
    4     33  oranges     33%
    

    What you tried just converted the column to a stringified version of a Series:

    In [12]:
    str(df['Grade'])
    
    Out[12]:
    '0    82\n1    38\n2    55\n3    19\n4    33\nName: Grade, dtype: int32'
    
    0 讨论(0)
提交回复
热议问题