pandas 如何添加熊猫数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17226024/
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-09-13 20:56:30 来源:igfitidea点击:
How to prepend pandas data frames
提问by TheoretiCAL
How can I prepend a dataframe to another dataframe? Consider dataframe A:
如何将数据帧添加到另一个数据帧?考虑数据框 A:
b c d
2 3 4
6 7 8
and dataFrame B:
和数据帧 B:
a
1
5
I want to prepend A to B to get:
我想将 A 加到 B 以获得:
a b c d
1 2 3 4
5 6 7 8
回答by Jeff
2 methods:
2种方法:
In [1]: df1 = DataFrame(randint(0,10,size=(12)).reshape(4,3),columns=list('bcd'))
In [2]: df1
Out[2]:
b c d
0 5 9 5
1 8 4 0
2 8 4 5
3 4 9 2
In [3]: df2 = DataFrame(randint(0,10,size=(4)).reshape(4,1),columns=list('a'))
In [4]: df2
Out[4]:
a
0 4
1 9
2 2
3 0
Concating (returns a new frame)
连接(返回一个新框架)
In [6]: pd.concat([df2,df1],axis=1)
Out[6]:
a b c d
0 4 5 9 5
1 9 8 4 0
2 2 8 4 5
3 0 4 9 2
Insert, puts a series into an existing frame
插入,将系列放入现有框架中
In [8]: df1.insert(0,'a',df2['a'])
In [9]: df1
Out[9]:
a b c d
0 4 5 9 5
1 9 8 4 0
2 2 8 4 5
3 0 4 9 2
回答by TheoretiCAL
Achieved by doing
做到了
A[B.columns]=B

