use astype(np.int64)
s = pd.Series(['', 8.00735e+09, 4.35789e+09, 6.10644e+09])
mask = pd.to_numeric(s).notnull()
s.loc[mask] = s.loc[mask].astype(np.int64)
s
0
1 8007350000
2 4357890000
3 6106440000
dtype: object
Answer from piRSquared on Stack Overflowuse astype(np.int64)
s = pd.Series(['', 8.00735e+09, 4.35789e+09, 6.10644e+09])
mask = pd.to_numeric(s).notnull()
s.loc[mask] = s.loc[mask].astype(np.int64)
s
0
1 8007350000
2 4357890000
3 6106440000
dtype: object
In Pandas/NumPy, integers are not allowed to take NaN values, and arrays/series (including dataframe columns) are homogeneous in their datatype --- so having a column of integers where some entries are None/np.nan is downright impossible.
EDIT:data.phone.astype('object')
should do the trick; in this case, Pandas treats your column as a series of generic Python objects, rather than a specific datatype (e.g. str/float/int), at the cost of performance if you intend to run any heavy computations with this data (probably not in your case).
Assuming you want to keep those NaN entries, your approach of converting to strings is a valid possibility:
data.phone.astype(str).str.split('.', expand = True)[0]
should give you what you're looking for (there are alternative string methods you can use, such as .replace or .extract, but .split seems the most straightforward in this case).
Alternatively, if you are only interested in the display of floats (unlikely I'd suppose), you can do pd.set_option('display.float_format','{:.0f}'.format), which doesn't actually affect your data.
Try
df['Column'] = df['Column'].str.replace('\d+', '')
this will remove all digits in the column
Try something like this
Regular expression to_replace
df = pd.DataFrame({'A': ['bat', 'foo', 'bait'],
'B': ['abc', 'bar', 'xyz']})
df.replace(to_replace=r'^ba.$', value='new', regex=True)
...
A B
0 new abc
1 foo new
2 bait xyz
In your case it will be:
df.replace(to_replace=r'^Fa.$', value='Fall', regex=True)
...
...
For future, I suggest refer here about regular expressions: https://docs.python.org/3/library/re.html
Note: ^ (Caret.) Matches the start of the string, and in MULTILINE mode also matches immediately after each newline.
Python Pandas Dataframe Remove Float Trailing Zeros - Stack Overflow
How to terminate or remove the .0 point from an int.
floating point - How can I remove ".0" of float numbers? - Stack Overflow
python - How to remove unwanted `.0` s in pandas? - Stack Overflow
Hello everyone,
I am still new to python and learning.
So I practiced some exercises and made an app that calculates the percentage from the number the user enters.
My question use, how can I terminate the .0 part if the user enters an Int and keep the decimal part if they enter a float?
so for example, 5% of 100 is 5 ( Int)
and 5.1% of 100 is 5.1 (float)
Here's a function to format your numbers the way you want them:
def formatNumber(num):
if num % 1 == 0:
return int(num)
else:
return num
For example:
formatNumber(3.11111)
returns
3.11111
formatNumber(3.0)
returns
3
you can use string formatting
>>> "%g" % 1.1
'1.1'
>>> "%g" % 1.0
'1'
You seem to be confusing objects with their representations. This DataFrame holds floats. You can represent these how you like (as strings) but when you change display options the objects remain the same. Converting to a list sends these float objects into a python list which has it's own representation of floats (unrelated to pandas) and will always display the .0.
You can't change the formatting of a python list. It is inbuilt. You could convert these floats to ints, by doing
list(map(int, sample_df['gpid']))[0:2]
[10498310800133, 767838527881217]
but be aware you are making new objects, not simply changing their representations. This means any non-integer floats (ie. doesn't end in .0) will be converted to integers.
To convert the pandas values to strings, do
sample_df['gpid'] = sample_df['gpid'].apply(lambda f: format(f, '.0f'))
Output:
gpid
0 10498310800133
1 767838527881217
To force int use:
list(sample_df['gpid'].astype('int64'))[0:2]
Your column is obviously type float64 due to floating point numbers of nan.
I have a float formatted to 2 decimal places. I need to eliminate the 2nd decimal place if it's a "0" but still keep 2 decimal places open for when its 2 whole numbers.
number = float(25.20458)
print(format(number, ".2f"))
#Comes out as 25.20
#Need 25.2Windows 10 and Python 3.7
If want convert integers and floats numbers to strings with no trailing 0 use this with map or apply:
df = pd.DataFrame({'col1':[1.00, 1, 0.5, 1.50]})
df['new'] = df['col1'].map('{0:g}'.format)
#alternative solution
#df['new'] = df['col1'].apply('{0:g}'.format)
print (df)
col1 new
0 1.0 1
1 1.0 1
2 0.5 0.5
3 1.5 1.5
print (df['new'].apply(type))
0 <class 'str'>
1 <class 'str'>
2 <class 'str'>
3 <class 'str'>
Name: new, dtype: object
I think something like this should work:
if val.is_integer() == True :
val = int(val)
elif val.is_float() == True :
val = Decimal(val).normalize()
Assuming that val is a float value inside the dataframe's column. You simply cast the value to be integer.
For float value instead you cut extra zeros.
You could use %g to achieve this:
'%g'%(3.140)
or, with Python โฅ 2.6:
'{0:g}'.format(3.140)
or, with Python โฅ 3.6:
f'{3.140:g}'
From the docs for format: g causes (among other things)
insignificant trailing zeros [to be] removed from the significand, and the decimal point is also removed if there are no remaining digits following it.
Me, I'd do ('%f' % x).rstrip('0').rstrip('.') -- guarantees fixed-point formatting rather than scientific notation, etc etc. Yeah, not as slick and elegant as %g, but, it works (and I don't know how to force %g to never use scientific notation;-).
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
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,
})
