Python 附加具有相同列、不同顺序的两个数据框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21435176/
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-08-18 22:50:38  来源:igfitidea点击:

Appending two dataframes with same columns, different order

pythonjoinpandasappend

提问by redrubia

I have two pandas dataframes.

我有两个熊猫数据框。

noclickDF = DataFrame([[0,123,321],[0,1543,432]], columns=['click', 'id','location'])
clickDF = DataFrame([[1,123,421],[1,1543,436]], columns=['click', 'location','id'])

I simply want to join such that the final DF will look like:

我只是想加入这样的最终 DF 看起来像:

click  |  id   |   location
0         123        321
0         1543       432
1         421        123
1         436       1543

As you can see the column names of both original DF's are the same, but not in the same order. Also there is no join in a column.

如您所见,两个原始 DF 的列名相同,但顺序不同。列中也没有联接。

采纳答案by unutbu

You could also use pd.concat:

你也可以使用pd.concat

In [36]: pd.concat([noclickDF, clickDF], ignore_index=True)
Out[36]: 
   click    id  location
0      0   123       321
1      0  1543       432
2      1   421       123
3      1   436      1543

Under the hood, DataFrame.appendcalls pd.concat. DataFrame.appendhas code for handling various types of input, such as Series, tuples, lists and dicts. If you pass it a DataFrame, it passes straight through to pd.concat, so using pd.concatis a bit more direct.

在幕后,DataFrame.append调用pd.concat. DataFrame.append具有处理各种类型输入的代码,例如系列、元组、列表和字典。如果你给它传递一个 DataFrame,它会直接传递到pd.concat,所以使用pd.concat会更直接一些。

回答by greole

You can use append for that

您可以为此使用 append

 df = noclickDF.append(clickDF)
 print df 

    click    id  location
 0      0   123       321  
 1      0  1543       432
 0      1   421       123
 1      1   436      1543

and if you need you can reset the index by

如果您需要,您可以通过以下方式重置索引

df.reset_index(drop=True)
print df
   click    id  location
0      0   123       321
1      0  1543       432
2      1   421       123
3      1   436      1543

回答by Beau Hilton

For future users (sometime >pandas 0.23.0):

对于未来的用户(有时>pandas 0.23.0):

You may also need to add sort=Trueto sort the non-concatenation axis when it is not already aligned (i.e. to retain the OP's desired concatenation behavior). I used the code contributed above and got a warning, see Python Pandas User Warning. The code below works and does not throw a warning.

您可能还需要添加sort=True对尚未对齐的非串联轴进行排序(即保留 OP 所需的串联行为)。我使用了上面贡献的代码并收到了警告,请参阅Python Pandas 用户警告。下面的代码有效并且不会发出警告。

In [36]: pd.concat([noclickDF, clickDF], ignore_index=True, sort=True)
Out[36]: 
   click    id  location
0      0   123       321
1      0  1543       432
2      1   421       123
3      1   436      1543