简体   繁体   English

如何根据日期合并两个不同大小的数据框

[英]How to merge two differently sized dataframes based on date

I am trying to merge two dataframes based on a date column with this code:我正在尝试使用以下代码合并基于日期列的两个数据框:

data_df = (pd.merge(data, one_min_df, on='date', how='outer'))

The first dataframe has 3784 columns and the second dataframe has 3764. Every date in the second dataframe is also within the first dataframe. The first dataframe has 3784 columns and the second dataframe has 3764. Every date in the second dataframe is also within the first dataframe. I would like to get the dataframes to merge on the date column with any dates that the longer dataframe has being left as blank or NaN etc.我想让数据框在日期列上与较长的 dataframe 保留为空白或 NaN 等的任何日期合并。

The code I have here gives the 3764 values followed by 20 empty rows, rather than correctly matching them.我在这里的代码给出了 3764 个值,后跟 20 个空行,而不是正确匹配它们。

Try this:尝试这个:

data['date'] = pd.to_datetime(data['date'])
one_min_df['date'] = pd.to_datetime(one_min_df['date'])
data_df = pd.merge(data, one_min_df, on='date', how='left')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM