Pandas 将列名从一个数据帧复制到另一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56080859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:23:07  来源:igfitidea点击:

Pandas copy column names from one dataframe to another

pythonpandasdataframecolumnname

提问by balkon16

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

假设我们有两个Pandas数据框。第一个没有列名:

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 namesfrom the col_names_dfto no_col_names_dfso that the following data frame is created:

我想要做的是让副本列来自col_names_dfno_col_names_df这样下的数据帧创建:

    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_dfI get NaNs.

但不是来自no_col_names_dfI getNaN的值。

采纳答案by jo9k

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)

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
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

回答by yatu

The simplest way is to directly assign the columns of col_names_dfto the ones of no_col_names_df:

最简单的方法是直接指派的列col_names_df到的那些no_col_names_df

no_col_names_df.columns = col_names_df.columns

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

回答by Andy L.

This:

这个:

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

gives you all 'NaN' dataframe because you pass a dataframe to construct a new dataframe and assign new columnsto it. Pandas essentially constructs identical dataframe and does reindexalong axis 1on it. In other words, that command is equivalent to doing:

为您提供所有“NaN”数据帧,因为您传递了一个数据帧来构造一个新的数据帧并为其分配新的数据columns。Pandas基本构造相同的数据框并不会reindex一起axis 1就可以了。换句话说,该命令相当于执行以下操作:

no_col_names_df.reindex(col_names_df.columns, axis=1)

You need either change directly no_col_names_df.columnsor passing no_col_names_df.values

您需要直接更改no_col_names_df.columns或传递no_col_names_df.values

回答by Yuca

If you're getting nan then most likely the issue is the data parameter, try this:

如果你得到 nan 那么问题很可能是数据参数,试试这个:

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

output:

输出:

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