Pandas copy column names from one dataframe to another

balkon16 picture balkon16 · May 10, 2019 · Viewed 20.5k times · Source

Let's say that we have two pandas data frames. The first one hasn't got column names:

no_col_names_df = pd.DataFrame(np.array([[1,2,3], [4,5,6], [7,8,9]]))

The second has:

col_names_df = pd.DataFrame(np.array([[10,2,3], [4,45,6], [7,18,9]]),
                           columns=['col1', 'col2', 'col3'])

What I want to do is to get copy column names from the col_names_df to no_col_names_df so that the following data frame is created:

    col1    col2    col3
0   1       2       3
1   4       5       6
2   7       8       9

I've tried the following:

new_df_with_col_names = pd.DataFrame(data=no_col_names_df, columns=col_names_df.columns)

but instead of values from the no_col_names_df I get NaNs.

Answer

jo9k picture jo9k · May 10, 2019

Just like you have used columns from the dataframe with column names, you can use values from the dataframe without column names:

new_df_with_col_names = pd.DataFrame(data=no_col_names_df.values, columns=col_names_df.columns)


In [4]: new_df_with_col_names = pd.DataFrame(data=no_col_names_df, columns=col_names_df.columns)

In [5]: new_df_with_col_names
Out[5]:
   col1  col2  col3
0   NaN   NaN   NaN
1   NaN   NaN   NaN
2   NaN   NaN   NaN

In [6]: new_df_with_col_names = pd.DataFrame(data=no_col_names_df.values, columns=col_names_df.columns)

In [7]: new_df_with_col_names
Out[7]:
   col1  col2  col3
0     1     2     3
1     4     5     6
2     7     8     9