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
Pandas copy column names from one dataframe to another
提问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_df
to no_col_names_df
so that the following data frame is created:
我想要做的是让副本列名来自col_names_df
于no_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_df
I get NaN
s.
但不是来自no_col_names_df
I 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_df
to 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 columns
to it. Pandas essentially constructs identical dataframe and does reindex
along axis 1
on 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.columns
or 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