简体   繁体   English

列名称不同时如何将数据框追加到另一个(具有相同列大小)

[英]How to append a dataframe to another one (with the same column size) when the column names are different

Is there a way to merge two dataframes with the same number of columns but with different names? 有没有一种方法可以合并两个具有相同列数但名称不同的数据框? The concat , merge , and join methods do not seem to work for this task. concatmergejoin方法似乎不适用于此任务。 For example, I want to combine these two: 例如,我要结合这两个:

#dataframe1:
columnA | columnB
15      | 23

#dataframe2:
columnC | columnD
12      | 29

Into this: 变成这个:

#dataframe3:
anyColumnName1 | anyColumnName1
15             | 23
12             | 29

Please note that I have 90 columns in my case. 请注意,我的情况下有90列

You could rename the columns of the second dataframe first. 您可以先重命名第二个数据框的列。

If they have the same number of columns then simply: 如果它们具有相同的列数,则只需:

dataframe2.columns = dataframe1.columns

and then concat them. 然后吸引他们。

You can use numpy.concatenate with DataFrame constructor: 您可以将numpy.concatenateDataFrame构造函数一起使用:

print (np.concatenate([df1, df2]))
[[15 23]
 [12 29]]

df = pd.DataFrame(np.concatenate([df1, df2]), columns=['a','b'])
print (df)
    a   b
0  15  23
1  12  29

You could use append by renaming columns of second dataframe based on first. 您可以通过基于第一个重命名第二个数据框的列来使用append

In [304]: df1.append(df2.rename(columns=dict(zip(df2.columns, df1.columns))))
Out[304]:
   columnA  columnB
0       15       23
0       12       29

Details. 细节。

In [308]: df1
Out[308]:
   columnA  columnB
0       15       23

In [309]: df2
Out[309]:
   columnC  columnD
0       12       29

In [310]: dict(zip(df2.columns, df1.columns))
Out[310]: {'columnC': 'columnA', 'columnD': 'columnB'}

In [311]: df2.rename(columns=dict(zip(df2.columns, df1.columns)))
Out[311]:
   columnA  columnB
0       12       29

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 append 一个 dataframe 变成另一个 dataframe 作为一列 - How to append one dataframe into another dataframe as a column 如何 append 两个 dataframe 对象包含相同的列数据但不同的列名? - How to append two dataframe objects containing same column data but different column names? 如何将一个数据帧的列值附加到另一个数据帧的列 - How to append column values of one dataframe to column of another dataframe 如何将行从一个 Dataframe 附加到另一个具有不同列结构的行 - How to append rows from one Dataframe to another having different column structure 如何从不同大小的另一个 dataframe 将列添加到 dataframe - How to add a column to a dataframe from another dataframe with different size 仅将具有列名的数据框追加到具有数据的另一个数据框 - Append dataframe with column names alone to another dataframe with data 如果索引值相同,如何将一个DataFrame列复制到另一个Dataframe中 - How to copy one DataFrame column in to another Dataframe if their indexes values are the same 如何从一个数据框中的列中提取特定值并将它们附加到另一个数据框中的列中? - 熊猫 - How do you extract specific values from a column in one dataframe and append them to a column in another dataframe? - Pandas 如何将两个不同的Dataframe列均等化为相同大小 - How to equalise two different Dataframe column into same size 将新的Pandas DataFrame附加到旧的,而没有对列名称进行排序的情况 - Append new Pandas DataFrame to an old one without column names sorted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM