Home > front end >  Clean way to convert string containing decimal to a string containing an int for a column in pandas?
Clean way to convert string containing decimal to a string containing an int for a column in pandas?

Time:11-10

I have a dataframe where one column contains numbers but as string values like "1.0", "52.0" etc. I want to convert the column to instead contain strings like "PRE_1", "PRE_52".

Example

df = pd.DataFrame({'pre': ["1.0", "52.0"]})
df["pre"] = 'PRE_'   df["pre"].astype(str)

gives me output of PRE_1.0

I tried: df["pre"] = 'PRE_' df["pre"].astype(int).astype(str) but got a ValueError.

Do I need to convert it into something else before trying to convert it to an int? It looks like: df["pre"].astype(float).astype(int).astype(str) might do what I want but I'm open to cleaner ways of doing it.

I'm pretty new to pandas, so help would be greatly appreciated!

CodePudding user response:

To properly be able to help, having sample data would be great. Based on the information you did provide, if the data coming in is a float, you can apply a format to truncate it as below.

df = pd.DataFrame({'pre': [1.0, 52.0]})
df['pre'] = df['pre'].map('PRE_{:.0f}'.format)
print(df)

CodePudding user response:

Apply a function:

import pandas as pd

df = pd.DataFrame([['1.0'],['52.0']],columns=['Pre'])
print(df)
df.Pre = df.Pre.apply(lambda n: f'PRE_{float(n):.0f}')
print(df)

Output:

    Pre
0   1.0
1  52.0
      Pre
0   PRE_1
1  PRE_52
  • Related