Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert data types of a Pandas dataframe to match another

Tags:

python

pandas

I have two data frames with the same column names but different data types:

df1.dtypes

order                                                                                                             
int64
x                                                                                                                                
int64
y                                                                                                                     
int64

df2.dtypes

order                                                                                                            
object
x                                                                                                                    
object
y                                                                                                                    
object

The dataframes are much larger than this, so I would like to capture the names/dtypes of df1 and convert df2 to match.

like image 930
Cody Avatar asked Jan 19 '18 19:01

Cody


People also ask

How do you convert a DataFrame data type?

The best way to convert one or more columns of a DataFrame to numeric values is to use pandas.to_numeric() . This function will try to change non-numeric objects (such as strings) into integers or floating-point numbers as appropriate.

How can I replace all values in a DataFrame with another value?

Suppose that you want to replace multiple values with multiple new values for an individual DataFrame column. In that case, you may use this template: df['column name'] = df['column name']. replace(['1st old value', '2nd old value', ...], ['1st new value', '2nd new value', ...])


2 Answers

The short version:

df2 = df2.astype(df1.dtypes.to_dict())
like image 84
Pete C Avatar answered Oct 24 '22 20:10

Pete C


dtypes + name and using astype convert

df=pd.DataFrame({'title':list('ABC'),'TestData':['Test1(data)','t(data2)','Ts(data 3)'],'Value':[1,2,3]})
df1=pd.DataFrame({'title':list('ABC'),'TestData':['Test1(data)','t(data2)','Ts(data 3)'],'Value':['1','2','3']})

df.dtypes
Out[287]:
TestData    object
Value        int64
title       object
dtype: object
df1.dtypes
Out[288]:
TestData    object
Value       object
title       object
dtype: object

for x in df1.columns:
    df[x]=df[x].astype(df1[x].dtypes.name)
df.dtypes
Out[290]: 
TestData    object
Value       object
title       object
dtype: object
like image 38
BENY Avatar answered Oct 24 '22 20:10

BENY