本文介绍了 pandas 如何在不丢失列标题的情况下合并两个数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下玩具代码:

 import pandas as pd
 df = pd.DataFrame()
 df["foo"] = [1,2,3,4]

 df2 = pd.DataFrame()
 df2["bar"]=[4,5,6,7]

 df = pd.concat([df,df2], ignore_index=True,axis=1)
 print(list(df))

输出:[0,1]
预期输出:[foo,bar](顺序不重要)
如果可以保证标头是唯一的,是否有任何方法可以串联两个数据框而又不丢失原始列标头?
反复考虑一下这些列,然后将它们添加到DataFrame之一中,但是是否有一个pandas函数或我不知道的concat参数?

Output: [0,1]
Expected Output: [foo,bar] (order is not important)
Is there any way to concatenate two dataframes without losing the original column headers, if I can guarantee that the headers will be unique?
Iterating through the columns and then adding them to one of the DataFrames comes to mind, but is there a pandas function, or concat parameter that I am unaware of?

谢谢!

推荐答案

合并,加入和合并文档,忽略索引将删除所有名称引用,而改用范围(0 ... n-1).因此,一旦删除ignore_index参数或将其设置为false(默认值),它应该会为您提供所需的结果.

As stated in merge, join, and concat documentation, ignore index will remove all name references and use a range (0...n-1) instead. So it should give you the result you want once you remove ignore_index argument or set it to false (default).

df = pd.concat([df, df2], axis=1)

这将基于索引将df和df2连接起来(相同的索引行将被串联,如果其他数据框没有该索引的成员,它将被串联为nan).

This will join your df and df2 based on indexes (same indexed rows will be concatenated, if other dataframe has no member of that index it will be concatenated as nan).

如果您在数据帧上具有不同的索引,并希望以这种方式进行连接.您可以创建一个临时索引并在其上进行连接,也可以在使用concat(...,ignore_index = True)之后设置新数据框的列.

If you have different indexing on your dataframes, and want to concatenate it this way. You can either create a temporary index and join on that, or set the new dataframe's columns after using concat(..., ignore_index=True).

这篇关于 pandas 如何在不丢失列标题的情况下合并两个数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:19