You have a few options...
1) convert everything to integers.
df.astype(int)
<=35 >35
Cut-off
Calcium 0 1
Copper 1 0
Helium 0 8
Hydrogen 0 1
2) Use round:
>>> df.round()
<=35 >35
Cut-off
Calcium 0 1
Copper 1 0
Helium 0 8
Hydrogen 0 1
but not always great...
>>> (df - .2).round()
<=35 >35
Cut-off
Calcium -0 1
Copper 1 -0
Helium -0 8
Hydrogen -0 1
3) Change your display precision option in Pandas.
pd.set_option('precision', 0)
>>> df
<=35 >35
Cut-off
Calcium 0 1
Copper 1 0
Helium 0 8
Hydrogen 0 1
Answer from Alexander on Stack OverflowYou have a few options...
1) convert everything to integers.
df.astype(int)
<=35 >35
Cut-off
Calcium 0 1
Copper 1 0
Helium 0 8
Hydrogen 0 1
2) Use round:
>>> df.round()
<=35 >35
Cut-off
Calcium 0 1
Copper 1 0
Helium 0 8
Hydrogen 0 1
but not always great...
>>> (df - .2).round()
<=35 >35
Cut-off
Calcium -0 1
Copper 1 -0
Helium -0 8
Hydrogen -0 1
3) Change your display precision option in Pandas.
pd.set_option('precision', 0)
>>> df
<=35 >35
Cut-off
Calcium 0 1
Copper 1 0
Helium 0 8
Hydrogen 0 1
Since pandas 0.17.1 you can set the displayed numerical precision by modifying the style of the particular data frame rather than setting the global option:
import pandas as pd
import numpy as np
np.random.seed(24)
df = pd.DataFrame(np.random.randn(5, 3), columns=list('ABC'))
df

df.style.set_precision(2)

It is also possible to apply column specific styles
df.style.format({
'A': '{:,.1f}'.format,
'B': '{:,.3f}'.format,
})

You need to re-assign dataframe
(which is, what I suppose your error is):
>>> import pandas as pd
>>> df = pd.DataFrame(data={"col": [24.00, 2.00, 3.00]})
>>> df.dtypes
col float64
dtype: object
>>> df
col
0 24.0
1 2.0
2 3.0
>>> df=df.astype(int)
>>> df
col
0 24
1 2
2 3
>>> df.dtypes
col int32
dtype: object
You can solve this by setting the pandas option, precision to 0.
import pandas as pd
df = pd.DataFrame(data={"col": [24.00, 2.00, 3.00]})
print(df)
col
0 24.0
1 2.0
2 3.0
pd.set_option('precision',0)
print(df)
col
0 24
1 2
2 3
I need to remove decimals from float to get 6 characters after the dot WITHOUT rounding For example I have 0.00655379 and I need to get 0.006553
my boyfriend has moved his excel table to python but it has added .0 to his values (eg 160 becomes 160.0) is there anyway to fix this and remove decimals ?
Use a function and apply to whole column:
In [94]:
df = pd.DataFrame({'Movies':['Save the last dance', '2012.0']})
df
Out[94]:
Movies
0 Save the last dance
1 2012.0
[2 rows x 1 columns]
In [95]:
def trim_fraction(text):
if '.0' in text:
return text[:text.rfind('.0')]
return text
df.Movies = df.Movies.apply(trim_fraction)
In [96]:
df
Out[96]:
Movies
0 Save the last dance
1 2012
[2 rows x 1 columns]
Here is hint for you ,
In case of Valid number ,
a="2012.0"
try:
a=float(a)
a=int(a)
print a
except:
print a
Output:
2012
In case of String like "Dance with Me"
a="Dance with Me"
try:
a=float(a)
a=int(a)
print a
except:
print a
Output:
Dance with Me
You can call int() on the end result:
>>> int(2.0)
2
When a number as a decimal it is usually a float in Python.
If you want to remove the decimal and keep it an integer (int). You can call the int() method on it like so...
>>> int(2.0)
2
However, int rounds down so...
>>> int(2.9)
2
If you want to round to the nearest integer you can use round:
>>> round(2.9)
3.0
>>> round(2.4)
2.0
And then call int() on that:
>>> int(round(2.9))
3
>>> int(round(2.4))
2
You can try using as df['col'] = (df['col']*100).astype(int)
as below:
df = pd.DataFrame({'col': [1.10, 2.20, 3.30, 4.40]})
df['col'] = (df['col']*100).astype(int)
print(df)
Output:
col
0 110
1 220
2 330
3 440
If - as your comment suggests - the data just all needs to be multiplied by 100...
df['columnName'] = df['columnName'].apply(lambda x: x*100)
The DataFrame round method should work...
import numpy as np
import pandas as pd
some_numbers = np.random.ranf(5)
df = pd.DataFrame({'random_numbers':some_numbers})
rounded_df = df.round(decimals=2)
import numpy as np
import pandas as pd
#fileName
f=001.csv
#Load File to Df
Urbs_Data=pd.read_csv(f,header=None)
#Round of all the numeric values to the specified decimal value
Urbs_Data= Urbs_Data.round(decimals=3)
This rounding off will be applied on all the Numeric columns
You can convert the type with .astype
In [312]: df.columns = df.columns.astype(int)
In [313]: df
Out[313]:
2006 2007 2008 2009
0 foo foo bar bar
1 foo foo bar bar
Or use .map and convert to string type.
In [338]: df.columns.map('{:g}'.format)
Out[338]: Index(['2006', '2007', '2008', '2009'], dtype='object')
In [319]: df.columns.map(int)
Out[319]: Int64Index([2006, 2007, 2008, 2009], dtype='int64')
You can convert to float before converting to int and then str:
df.columns = df.columns.values.astype(float).astype(int).astype(str)
print(df.columns)
Index(['2006', '2007', '2008', '2009'], dtype='object')
Seems long-winded, but at least we are working with the underlying NumPy array.