Search code examples
pythonpandasstringdataframedata-conversion

How do I convert a dataframe column filled with numbers to strings in python?


I have a dataframe dfA that has a column diff where every value is a number, example:

dfA['diff']

Outputs:

88    -120.0
89    -130.0
90    -105.0
91    -115.0
92    -120.0
93    -110.0
94    -120.0
95    -115.0
96    -120.0
97    -105.0
98    -130.0
99    -115.0
100   -115.0
101    120.0
102   -155.0
103    115.0
104   -150.0
105   -190.0
106    140.0
107    170.0
108   -240.0
109    115.0
110   -160.0
111   -125.0
112   -110.0
115   -205.0
116    150.0
117   -155.0
118    115.0
Name: diff, dtype: float64

I want to:

  • Remove the decimal
  • Add a + sign in front if diff is a positive number
  • And finally convert to a string

Example:

88    -120
89    -130
90    -105
91    -115
92    -120
93    -110
94    -120
95    -115
96    -120
97    -105
98    -130
99    -115
100   -115
101   +120
102   -155
103   +115
104   -150
105   -190
106   +140
107   +170
108   -240
109   +115
110   -160
111   -125
112   -110
115   -205
116   +150
117   -155
118   +115
  • I've tried using the int() function but I receive a TypeError: cannot convert the series to <class 'int'>

  • I'm not sure how to add the '+' sign to positive numbers

  • I do know that dfA['diff'] = dfA['diff'].apply(str) is the string conversion portion

Please help, thank you!


Solution

  • Convert values to integers and strings to helper Series s, so possible compare original column for greater like 0 with Series.mask for prepend +:

    s = dfA['diff'].astype(int).astype(str)
    dfA['diff'] = s.mask(dfA['diff'].gt(0), '+' + s)
    
    print (dfA)
         diff
    98   -130
    99   -115
    100  -115
    101  +120
    102  -155
    103  +115
    104  -150
    

    With formatting:

    dfA['diff'] = dfA['diff'].map(lambda x: '{0:+}'.format(int(x)))