简体   繁体   English

根据 Date 列将 df1 中的列添加到 df2 中,如果缺少 df1 [Date] 条目,则填写 na

[英]Add column from df1 into df2 based on the Date column and fill na if df1 [Date] entries are missing

I have dataframe df1 with 850 rows and column names ['Date', 'A'] .我有df1具有 850 行和列名['Date', 'A']数据框。 I also have df2 with 900 rows and column names ['Date', 'B', 'C', 'D'] .我也有df2 900 行和列名['Date', 'B', 'C', 'D']

The difference in their number of rows is because df1 has some missing 'Date' entries.它们的行数不同是因为df1缺少一些“日期”条目。 But, all entries in df1['Date'] are in df2['Date'].但是,df1['Date'] 中的所有条目都在 df2['Date'] 中。

Question: I would like to merge df1['A'] to df2 on basis of same ['Date'] rows.问题:我想基于相同的['Date']行将df1['A']合并到df2 After merging, I would like the resultant df2['A'] to reflect a 'na' for all those rows whose ['Dates'] are missing in df1 .合并后,我希望生成的df2['A']df1中缺少['Dates']的所有行反映一个 'na' 。

I tried df2=pd.merge(df2, df1, on="Date") but I get resultant df2 to have 850 rows which seems that the dates which don't match between df1 and df2 are being deleted.我尝试df2=pd.merge(df2, df1, on="Date")但我得到的结果df2有 850 行,这似乎是删除了 df1 和 df2 之间不匹配的日期。 Instead, I would want the post-merged resultant df2 to be 900 rows and the unmatched date rows should show 'na' in df2['A']`.相反,我希望合并后的结果df2为 900 行,并且不匹配的日期行应在 df2['A']` 中显示“na”。

How to achieve this?如何做到这一点?

Use left join instead of inner join (default behavior)使用left连接而不是inner连接(默认行为)

ie, IE,

new_df = pd.merge(df2, df1, on="Date", how='left')

To fill NA (as asked by OP in comments) with zero,用零填充NA (如 OP 在评论中要求的那样),

new_df.fillna(0, inplace=True)
# new_df['column'] = new_df['column'].astype(np.float64) # to convert column to float

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 df2 中的日期早于 df1 中的日期时,如何检查 df1 中的 id 是否存在于 df2 中,并使用 pandas 相应地添加新列 - how to check whether the id in df1 existed in df2 when date in df2 is older than date in df1 and add new column accordingly using pandas 根据 df2 中的相应列舍入 df1 中的每一列 - Round each column in df1 based on corresponding column in df2 如果 df2 中不存在列,如何将列从 df1 添加到 df2,否则什么也不做 - How to add a column from df1 to df2 if it not present in df2, else do nothing 使用包含 2 个数据框和日期范围的 IP 列来使用来自 df2 的数据填充 df1 数据框 - Use IP column of 2 dataframes and date range to populate df1 dataframe with data from df2 根据日期索引上的df1更新df2中的列 - Update columns in df2 based on df1 on index of date 根据 df2 中的条件查找 df1 中的列值 - Looking up column value in df1 based on criteria in df2 根据 df1 中的值在 df2 中保留一列 - Keep one column in df2 based on value in df1 如何通过匹配 df1 中与 df2 索引和列名匹配的列值来用 df1 中的数据填充 df2 - How to fill df2 with data from df1 by matching column values from df1 which match df2 index and column names 如何向 dataframe (df1) 添加一个新列,这是另一个 dataframe (df2) 中 df1 的多个查找值的总和 - How can I add a new column to a dataframe (df1) that is the sum of multiple lookup values from df1 in another dataframe (df2) 如何用来自另一个 dataframe (df2) 的信息填充 dataframe (df1) 的列? 就在 df1 和 df2 中的两列信息匹配时? - How to fill a column of a dataframe (df1) with info from another dataframe (df2)? Just when two column info matches in df1 and df2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM