I'm reading several spreadsheets into a data frame and there is an ID field that is a string in some spreadsheets and a number in others. I've converted it into a string, which is the data type I need, but I'm ending up with some IDs that have a ".0" at the end. How do I remove the decimal and zero?
Example: ID number 805096730.0 should be 805096730
Use the str() class to convert the decimal to a string. Use the str. rstrip() method to strip the trailing zeros if the number has a decimal point.To remove trailing zeros from a decimal: Use the decimal.
Using trunc() Function In the first program, we will make use of trunc() function and remove the decimal present in the numbers.
Right click on one of the decimal value on the graph and go to format y/x axis and under number tab you have an option to make the decimal places to 0.
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})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With