Python 将列附加到 Pandas 数据框

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

Append column to pandas dataframe

pythonpandas

提问by BenDundee

This is probably easy, but I have the following data:

这可能很容易,但我有以下数据:

In data frame 1:

在数据框 1 中:

index dat1
0     9
1     5

In data frame 2:

在数据框 2 中:

index dat2
0     7
1     6

I want a data frame with the following form:

我想要一个具有以下形式的数据框:

index dat1  dat2
0     9     7
1     5     6

I've tried using the appendmethod, but I get a cross join (i.e. cartesian product).

我试过使用该append方法,但我得到了一个交叉连接(即笛卡尔积)。

What's the right way to do this?

这样做的正确方法是什么?

采纳答案by U2EF1

It seems in general you're just looking for a join:

一般来说,您似乎只是在寻找加入:

> dat1 = pd.DataFrame({'dat1': [9,5]})
> dat2 = pd.DataFrame({'dat2': [7,6]})
> dat1.join(dat2)
   dat1  dat2
0     9     7
1     5     6

回答by BenDundee

Just a matter of the right google search:

只是一个正确的谷歌搜索问题:

data = dat_1.append(dat_2)
data = data.groupby(data.index).sum()

回答by Ella Cohen

You can also use:

您还可以使用:

dat1 = pd.concat([dat1, dat2], axis=1)

回答by Jeremy Z

Both join()and concat()way could solve the problem. However, there is one warning I have to mention: Reset the index before you join()or concat()if you trying to deal with some data frame by selecting some rows from another DataFrame.

无论join()concat()方法可以解决这个问题。但是,我必须提到一个警告:在您之前重置索引,join()或者concat()如果您尝试通过从另一个 DataFrame 中选择一些行来处理某个数据框。

One example below shows some interesting behavior of join and concat:

下面的一个例子展示了 join 和 concat 的一些有趣的行为:

dat1 = pd.DataFrame({'dat1': range(4)})
dat2 = pd.DataFrame({'dat2': range(4,8)})
dat1.index = [1,3,5,7]
dat2.index = [2,4,6,8]

# way1 join 2 DataFrames
print(dat1.join(dat2))
# output
   dat1  dat2
1     0   NaN
3     1   NaN
5     2   NaN
7     3   NaN

# way2 concat 2 DataFrames
print(pd.concat([dat1,dat2],axis=1))
#output
   dat1  dat2
1   0.0   NaN
2   NaN   4.0
3   1.0   NaN
4   NaN   5.0
5   2.0   NaN
6   NaN   6.0
7   3.0   NaN
8   NaN   7.0

#reset index 
dat1 = dat1.reset_index(drop=True)
dat2 = dat2.reset_index(drop=True)
#both 2 ways to get the same result

print(dat1.join(dat2))
   dat1  dat2
0     0     4
1     1     5
2     2     6
3     3     7


print(pd.concat([dat1,dat2],axis=1))
   dat1  dat2
0     0     4
1     1     5
2     2     6
3     3     7

回答by Raj Stha

Just as a matter of fact:

事实上:

data_joined = dat1.join(dat2)
print(data_joined)