Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas convert float to int

df['col'] = df['col'].astype(int)
Comment

convert float to integer pandas

>>> df['C'] = df['C'].apply(np.int64)
>>> print(df)
...    A  B  C         D
... 0  8  0  1  6.226750
... 1  1  9  9  8.522808
... 2  1  4  2  7.739108
Comment

convert float to integer pandas

>>> df
          A         B     C         D
0  0.475103  0.355453  0.66  0.869336
1  0.260395  0.200287   NaN  0.617024
2  0.517692  0.735613  0.18  0.657106
>>> df[list("ABCD")] = df[list("ABCD")].fillna(0.0).astype(int)
>>> df
   A  B  C  D
0  0  0  0  0
1  0  0  0  0
2  0  0  0  0
Comment

object to int and float conversion pandas

In [39]:

df['2nd'] = df['2nd'].str.replace(',','').astype(int)
df['CTR'] = df['CTR'].str.replace('%','').astype(np.float64)
df.dtypes
Out[39]:
Date         object
WD            int64
Manpower    float64
2nd           int32
CTR         float64
2ndU        float64
T1            int64
T2            int64
T3            int64
T4           object
dtype: object
In [40]:

df.head()
Out[40]:
        Date  WD  Manpower   2nd   CTR  2ndU   T1    T2   T3     T4
0   2013/4/6   6       NaN  2645  5.27  0.29  407   533  454    368
1   2013/4/7   7       NaN  2118  5.89  0.31  257   659  583    369
2  2013/4/13   6       NaN  2470  5.38  0.29  354   531  473    383
3  2013/4/14   7       NaN  2033  6.77  0.37  396   748  681    458
4  2013/4/20   6       NaN  2690  5.38  0.29  361   528  541    381
Comment

how to change data type from int to float in dataframe

df['DataFrame Column'] = df['DataFrame Column'].astype(float)
Comment

pandas changing float to int

# for x axis
ax.xaxis.set_major_formatter(FuncFormatter(lambda x, _: int(x)))

# for y axis
ax.yaxis.set_major_formatter(FuncFormatter(lambda y, _: int(y)))
Comment

PREVIOUS NEXT
Code Example
Python :: Write a Python program to get the Python version you are using. 
Python :: delete an element by value from a list if it made of white spaces python 
Python :: how to press enter in selenium python 
Python :: xor string python 
Python :: argparse required arguments 
Python :: get span text selenium python 
Python :: what is hashlib in python 
Python :: streamlit python install 
Python :: tkinter entry 
Python :: pip install qrcode python 
Python :: python how to get pixel values from image 
Python :: alphabet python 
Python :: python list only files not directories 
Python :: how to make table using python 
Python :: print last exceuted query python 
Python :: yahoo finance api python 
Python :: count number of spaces in string python 
Python :: django save vs create 
Python :: django unique together 
Python :: how to rename rengeindex pandas 
Python :: remove empty space from string python 
Python :: how to take input in python3 separated by space 
Python :: how to convert decimal to binary python 
Python :: python convert string to byte array 
Python :: regular expression to remove space python 
Python :: how to convert a set to a list in python 
Python :: reverse the words in a string python 
Python :: flask heroku 
Python :: python check if array is subset of another 
Python :: install tensorflow gpu 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =