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,
})

python - Deleting decimals from a pandas dataframe - Stack Overflow
how to remove decimal points from values ?
How to remove the decimal places while using st.table?
pandas - How to remove extra decimals from the column values if occurs in python - Stack Overflow
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
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 ?
Regex101:
df["column1"] = df["column1"].str.replace(r"(\.)(?=.*\.)", "", regex=True)
print(df)
Prints:
column1
0 27356.10
1 25689
2 2345.00
3 NaN
4 12325.70
5 1234575.00
Alternatively, you can choose not to use Regex:
df["column1"] = df["column1"].str.replace('.', '', df["column1"].count('.')-1)
I think this reads more intuitively -- replace . with {empty} {count-of-periods-used}-1 times
inputs = """27.356.10
25689
2345.00
NaN
123.25.70
12.345.75.00""".split('\n')
print(inputs)
for input in inputs:
print(input.replace('.', '', input.count('.')-1))
['27.356.10', '25689', '2345.00', 'NaN', '123.25.70', '12.345.75.00']
27356.10
25689
2345.00
NaN
12325.70
1234575.00
Use astype with replace:
df = pd.DataFrame({'ID':[805096730.0,805096730.0]})
df['ID'] = df['ID'].astype(str).replace('\.0', '', regex=True)
print (df)
ID
0 805096730
1 805096730
Or add parameter dtype:
df = pd.read_excel(file, dtype={'ID':str})
Check type of your numbers before converting them to strings. It seems that they are floats, rather than integers. If this is the case, convert your numbers to integers:
df = pd.DataFrame([123.0, 456.0])
df = df.apply(int, axis=1)
0 123
1 456
Then, convert it into strings:
df = df.apply(str)
print(df.iloc[1])
'456'
Try the below:
m=(df.dtypes=='float')
df.loc[:,m]=df.loc[:,m].astype(int)
print(df)
query qstart qend name number strand
0 A 2 1064 None 0 +
1 B 2 1076 None 0 +
2 C 2 1064 None 0 +
3 D 0 741 None 0 +
df.qend = df.qend.apply(int) # round instead of int is you prefer to round
print (df)
query qstart qend name number strand
0 A 2.0 1064 None 0 +
1 B 2.0 1076 None 0 +
2 C 2.0 1064 None 0 +
3 D 0.0 741 None 0 +
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.
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)