Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to convert data type of a column in pandas

# You can use "astype" method
# suppose you want to correct your "sales" column data type
df['sales'] = df['sales'].astype('float64')
Comment

change dataframe column type

>>> df.astype({'col1': 'int32'}).dtypes
col1    int32
col2    int64
dtype: object
Comment

pandas change column dtype

# Individual  
df['a'] = df_test['a'].astype('float64')
df['b'] = df_test['b'].astype('int64')
# Batch
dtype_d = {"a": "float64", "b": "int64"} # dtype dictio
df = df.astype(dtype_d)
Comment

set type of column pandas

df.astype(int)
Comment

set column datatype pandas

df = pd.read_csv("weather.tsv", sep="	",  
                 dtype={'Day': str,'Wind':int64})
df.dtypes
Comment

pandas change column type

# select columns that need to be converted
cols = df.select_dtypes(include=['float64']).columns.to_list()
df = df.astype({col:int for col in cols})
Comment

how to change datatype of column in pandas

convert pandas datatype
Comment

dataframe change column types

df['current_anniversary_date'] = df['current_anniversary_date'].astype('datetime64[ns]')
Comment

PREVIOUS NEXT
Code Example
Python :: _csv.Error: field larger than field limit (131072) 
Python :: pandas index to list 
Python :: string array to float array python 
Python :: ionic python2 Error: not found: python2 
Python :: how to access for loop counter of outer loop 
Python :: pandas drop values from column 
Python :: create new thread python 
Python :: py spam message 
Python :: check package version python 
Python :: how to trim mp4 with moviepy 
Python :: heroku change python version 
Python :: install python decouple 
Python :: how to raise a error in python 
Python :: cv2 videocapture nth frame 
Python :: python discord discord.py disable remove help command 
Python :: convert python pandas series dtype to datetime 
Python :: filter dataframe by index 
Python :: password manager python with min and max pass lenght 
Python :: how to change opencv capture resolution 
Python :: python clear screen 
Python :: from csv to pandas dataframe 
Python :: djangodebug toolbar not showing 
Python :: flatten a list of list python 
Python :: python bisection method 
Python :: lisy in python 
Python :: remainder identifying python 
Python :: how to make a PKCS8 RSA signature in python 
Python :: python wget anaconda 
Python :: Python Time object to represent time 
Python :: install decouple python 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =