0

I have some columns in a pandas dataframe consisting of strings, which I want to convert to numbers (float). The problem is the following: some entries consist of just '-' and others are like '1E-06'. Is there an easy way to replace just the '-' symbol, e.g. with a number -- using for example df['col'].str.replace('-','1000'), without modifying the string '1E-06'? Otherwise, '1E-06' will be also modified, e.g. into '1E100006', which will then affect the subsequent conversion into float numbers.

llllllllll
  • 16,169
  • 4
  • 31
  • 54
MarcoC
  • 119
  • 10

2 Answers2

2

This is how I would do it:

df = pd.DataFrame({'value':['-','1e-06']})
df['value'] = df['value'].replace('-', '1000')

OR:

df['value'].replace('-', '1000', inplace=True)

Output:

value
100
1e-06
Ankur Sinha
  • 6,473
  • 7
  • 42
  • 73
1

I would do:

for string in your_stuff:
    string = string.replace('-','1000') if string == '-' else string;

alternatively use

map(lambda string: string.replace('-','1000') if string == '-' else string, your_stuff)
Attersson
  • 4,755
  • 1
  • 15
  • 29