Home > Blockchain >  Place decimal to correct location in a column of dataframe
Place decimal to correct location in a column of dataframe

Time:11-15

I have a dataframe amongst which there is a column where I have to put in decimal after 2 digits for all the rows:

I/P

A     B
1     15
2     346.52
3     25678

dtype for both is object
O/P required is:

A     B
1     15
2     34.652
3     25.678

In solution: Decimal after 2 digits

I tried removing the . and then trying to put 2 decimal spaces by converting it to str like this:

df[B] = df[B].str.replace('.','')
df[B] = df[B].astype(str)
df[B] = df[B].apply(lambda x: [(df[B][x].str[:2]   '.'   df[B][x].str[2:]).astype(float) for x in df[0]])

But this is not working, it kind of works on just one entry. Any idea here would help

CodePudding user response:

This is what I would suggest:

df['B'] = df['B'].str.replace('.', '', regex=False)
df.loc[df['B'].str.len() > 2, 'B'] = df['B'].apply(lambda x: '.'.join([x[:2], x[2:]]))

Output:

    A        B
0   1       15
1   2   34.652
2   3   25.678

At first we replace a dot with nothing like you wanted. When it is changed we take all elements which have length greater than 2 and join first two characters with rest of the string, with dot as a separator.

It can work if you are sure that dot should be always place after two digits. You need to be careful because it is possible that 167 will be a valid number. In this case we will convert it into 16.7, but if you've been trying to replace dot with nothing, I believe you are sure it should be done this way in your case.

You can convert this column to float at the end obviously.

  • Related