简体   繁体   English

熊猫基于其他两个“子”框架创建数据框架

[英]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

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.

相关问题 基于两个数据帧pandas python的最小值创建数据帧 - create a data frame based on the minimum value of two data frames pandas python 基于行索引将数据帧拆分为两个不相交的子帧 - Split data frame in two disjoint sub frames based on row index 熊猫数据框-合并两个基于“ InStr”> 0的数据框 - Pandas Data Frame - Merge Two Data Frames based on “InStr” > 0 基于第三个数据帧在 Pandas 中加入两个数据帧 - Join two Data Frames in Pandas based on third data frame 根据其他两个数据帧填充新的数据帧 - Filling in a new data frame based on two other data frames 根据其他两列中的字符串创建pandas数据框列 - Create pandas data frame column based on strings from two other columns 如何根据某些条件从大数据框创建子数据框? - How to create sub data frames from a large data frame based on some condition? 如何根据 Python Pandas 中第二个数据帧中的几列合并两个数据帧? - How to merge two Data Frames based on a few columns in second Data Frame in Python Pandas? 我有两个结构相似的数据框,我需要根据另一个更新其中一个数据框 - I have two data frames which are similar in structure, I need to update one of the data frame based on the other 尝试使用 Pandas 数据框中其他两列的 groupby 基于另一列创建新的滚动平均列时出错 - Error when trying to create new rolling average column based on another column using groupby of two other columns in pandas data frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM