Pandas 如何在不丢失列标题的情况下连接两个数据帧
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43404877/
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 how to concat two dataframes without losing the column headers
提问by Priyank
I have the following toy code:
我有以下玩具代码:
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))
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,1]
预期输出:([foo,bar]
顺序不重要)
有没有办法在不丢失原始列标题的情况下连接两个数据帧,如果我能保证标题是唯一的?
我想到了遍历列然后将它们添加到其中一个 DataFrame 中,但是是否有concat
我不知道的 Pandas 函数或参数?
Thanks!
谢谢!
回答by umutto
As stated in merge, join, and concatdocumentation, 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).
如合并、连接和连接文档中所述,忽略索引将删除所有名称引用并使用范围 (0...n-1) 代替。因此,一旦您删除ignore_index
参数或将其设置为 false(默认值),它就会为您提供所需的结果。
df = pd.concat([df, df2], axis=1)
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).
这将根据索引连接您的 df 和 df2(将连接相同的索引行,如果其他数据帧没有该索引的成员,它将连接为 nan)。
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).
如果您的数据帧上有不同的索引,并希望以这种方式连接它。您可以创建一个临时索引并在其上加入,或者在使用 concat(..., ignore_index=True) 后设置新数据框的列。