Home > Blockchain >  How to change 0.80 to 0.8 / 1.0 to 1 in dataframe?
How to change 0.80 to 0.8 / 1.0 to 1 in dataframe?

Time:01-28

I have a dataframe looks like this.

df = pd.DataFrame([0.70,1.0,0.75,0.0,5.0], columns=['pitch'])

I want to convert it into

df = pd.DataFrame([0.7,1,0.75,0,5], columns=['pitch'])

If i convert the float to int, 0.7 will be 0 .

How to solve this problem, thanks!!

CodePudding user response:

astype() e round

can use the astype(int) function in conjunction with the round() function on the dataframe. This will round each value in the 'pitch' column to the desired number of decimal places and then convert those rounded values to integers.

df['pitch'] = df['pitch'].round(1).astype(int)

or using round(0) to remove decimal places completely

df['pitch'] = df['pitch'].round(0).astype(int)

Be careful using the astype(int) function to convert to int, as it can round values, not just remove decimal places.

CodePudding user response:

You can use the round() function to round the decimal values to the desired number of decimal places before converting them to integers. For example, you can use round(df['pitch'], 1) to round the values in the 'pitch' column to one decimal place, then use astype(int) to convert them to integers.

df['pitch'] = round(df['pitch'], 1).astype(int)
  • Related