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.
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.
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', ...])
The short version:
df2 = df2.astype(df1.dtypes.to_dict())
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
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