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

sportzcode

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!

jezrael

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)))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert a WrappedArray column in spark dataframe to Strings?

How do I convert a dataframe of strings to csv in python?

How to convert a column of numbers into a date in a dataframe in python

How can I move the location points in numbers or strings of a DataFrame column in Python?

How do I convert a column having month in a dataframe to alphabetical numbers (not numeric) based on a specific condition

How do I convert strings in an array to numbers using forEach?

How do I extract numbers from the strings in a pandas column of 'object'?

How do I convert an RDD with a SparseVector Column to a DataFrame with a column as Vector

How do I convert this dictionary to a dataframe in python?

R: How do I convert a dataframe of strings into POSIXt objects?

Using Pandas in Python 3, how do I filter out repeat strings in a column within a dataframe?

How to convert column of strings into sequential numbers in Pandas?

How do I create a labeling column for strings based on another DataFrame?

How do I concatenate strings of rounded numbers python?

Python: In a DataFrame, how do I loop through all strings of one column and check to see if they appear in another column and count them?

How can I convert an array of numbers to strings?

How can I add a column to an existing filled dataframe

how to convert a column to numeric while it contains both strings and numbers as strings

How to convert numbers into strings in python? 1 -> 'one'

in python how do i convert a list of strings to pandas data frame

How do I convert a list of strings to integers in Python

In panda python how do I "blacklist" or "whitelist" numbers in a DataFrame

How do I convert an entire column to numeric based on whether the column is all numbers (but in character format)?

How do I convert a dataframe consisting of a column of sentences and a column of scores into one with a column of words and average scores?

Compare dataframe column with strings and numbers

How do i convert python list into pandas dataframe with predefined columns

How do I convert a Python DataFrame into a NumPy array

How to convert bytes into strings for entries in a dataframe (Python)?

Python: How to convert variables and their respective strings into a dataframe?