[英]Pandas create a data frame based on two other 'sub' frames
I have two Pandas data frames. 我有两个熊猫数据框。 df1 has columns ['a','b','c'] and df2 has columns ['a','c','d'].
df1具有列['a','b','c'],而df2具有列['a','c','d']。 Now, I create a new data frame df3 with columns ['a', b','c','d'].
现在,我用列['a',b','c','d']创建一个新的数据框df3。
I want to fill df3 with all the inputs from df1 and df2. 我想用df1和df2的所有输入填充df3。 For example, if I have x rows in df1, and y rows in df2, then I will have x+y rows in df3.
例如,如果我在df1中有x行,在df2中有y行,那么我在df3中将有x + y行。
Which Pandas function fills the new dataframe based on partial columns? 哪个Pandas函数基于部分列填充新的数据框?
Example data: 示例数据:
df1 = pd.DataFrame({'a':[1, 2, 3], 'b':[2, 3, 4], 'd':['h', 'j', 'k']})
df2 = pd.DataFrame({'a':[5, 6, 7], 'b':[1, 1, 1], 'c':[2, 2, 2]})
Code: 码:
df1.append(df2)
Out: 出:
a b c d
0 1 2 NaN h
1 2 3 NaN j
2 3 4 NaN k
0 5 1 2.0 NaN
1 6 1 2.0 NaN
2 7 1 2.0 NaN
use append function of dataframe https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.append.html 使用数据框的附加功能https://pandas.pydata.org/pandas-docs/stable/genic/pandas.DataFrame.append.html
anotherFrame = df1.append(df2, ignore_index=True)
another way is merge - https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html 另一种方法是合并-https: //pandas.pydata.org/pandas-docs/stable/genic/pandas.DataFrame.merge.html
df1.merge(df2, how='outer')
How about: 怎么样:
df1 = pd.DataFrame({"a": [1,2], "b": [3,4], "c": [5,6]})
df2 = pd.DataFrame({"a": [7,8], "c": [9,10], "d": [11,12]})
df3 = df1.append(df2, sort=False)
df3
a b c d
0 1 3.0 5 NaN
1 2 4.0 6 NaN
0 7 NaN 9 11.0
1 8 NaN 10 12.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.