Skip to content
Advertisement

match dtypes of one df to another with different number of columns

I have a dataframe that has 3 columns and looks like this:

name           date             result
Anya          2021-02-13          0
Frank         2021-02-14          1

The other dataframe looks like this:

name           date             
Anya          2021-02-13          
Frank         2021-02-14          

I need to match the data types of one df to another. Because I have one additional column in df_1 I got an error. My code looks like this:

df_1.info()
Data columns (total 3 columns):
 #   Column         Non-Null Count  Dtype 
---  ------         --------------  ----- 
 0   name           717 non-null    object
 1   date           717 non-null    object
 2   result         717 non-null    int64 

df_2.info()
Data columns (total 3 columns):
 #   Column         Non-Null Count  Dtype 
---  ------         --------------  ----- 
 0   name           717 non-null    object
 1   date           717 non-null    datetime64[ns]

# Match the primary df to secondary df
for x in df_1.columns:
    df_2[x] = df_2[x].astype(df_1[x].dtypes.name)

I got an error: KeyError: 'profitable' What would be a workaround here? I need the dtypes of df_2 to be exactly the same as df_1. Thanks!

Advertisement

Answer

df1->that has 3 columns
df2->other dataframe

Firstly make use of boolean mask to find out those columns which are common in both dataframes:

mask=df1.columns.isin(df2.columns)
df=df1[df1.columns[mask]]

Now finally make use of astype() method:

df2=df2.astype(df.dtypes)

Or you can do all this in 1 line by:

df2=df2.astype(df1[df1.columns[df1.columns.isin(df2.columns)]].dtypes)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement