How to return a string transformed column to a float, int?

Asked

Viewed 93 times

-1

I had to turn a column of a database into a string in order to remove the last digits of the values, which were 0.1, 0.2 or 0.3 and this would disturb my data in the future. However, by trying to reconnect this column to the previous database, the entire database has become something I can’t even read as a table.

How could I make this column become what it was before, but without such 0.1 numbers... in the end?

Follow a part of the code:

raiosplanetas=ascii.read('raios planetarios.txt', guess=False)

Nome_Simbad=str(raiosplanetas['SimbadName']).replace('.01' or '.02' or '0.3' or '0.4','')

raiosplanetas['Nome']=Nome_Simbad

The first line shows how I turned the column into a string and removed the values I didn’t want, and the second is trying to reintroduce it to the original table.

An example of what is changed with this replace:KOI-0001.01 function turns KOI-0001 and KOI-5877.03 turns KOI-5877

  • Name_simbad is a number or string?

  • Only with this piece of code is complicated. Put the full code for us to help you

  • And another thing. You can remove decimal numbers without having to pass to a string.

  • Felipe: Name_simbad apparently became a string when I did this transformation in the second line of the code. Max: I could stick to the complete table with the 'Simbadname' column, but in theory that part of the code is the total of the problem. The other way to remove the numbers would be like Ruben suggested below? I’m trying it.

1 answer

1


A simple example, by making an exchange, replace, Can, depending on the problem, remove after the point

primeira_lista = ['KOI-0001.01','KOI-5877.03']
segunda_lista = []

for pl in primeira_lista:
  segunda_lista.append(pl[0:pl.find('.')])

print(segunda_lista)

exit

['KOI-0001', 'KOI-5877']

now you want to keep in the same list

primeira_lista = ['KOI-0001.01','KOI-5877.03']

for i in range(0,len(primeira_lista)):
  primeira_lista[i] = primeira_lista[i][0:primeira_lista[i].find('.')]

print(primeira_lista)
  • Do the elements of a column fit as a list? And what would I put in place of pl, to search in the list?

  • It worked, the answers to my two questions: Yes, and leave pl. Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.