pd.concat()和pd.merge()之间的区别以及为什么我得到错误的输出?

我在需要加入两个数据框时遇到困难。我通常应用pd.merge()。但是在这种情况下,我遇到ValueError,建议使用pd.concat()。所以,我的情况是这样的:

我有两个数据帧df1和df2,它们是下面的索引。

In [15]: df1.index
Out[15]: 
DatetimeIndex(['2019-11-03 00:00:00','2019-11-03 01:00:00','2019-11-03 02:00:00','2019-11-03 03:00:00',...
               '2019-11-12 11:00:00','2019-11-12 12:00:00','2019-11-12 13:00:00','2019-11-12 14:00:00'],dtype='datetime64[ns]',name='datetime',length=231,freq=None)


In [16]: df2.index
Out[16]: 
Index(['2019-11-03 00:00:00','2019-11-04 00:00:00','2019-11-05 00:00:00','2019-11-06 00:00:00','2019-11-07 00:00:00','2019-11-08 00:00:00','2019-11-09 00:00:00','2019-11-10 00:00:00','2019-11-11 00:00:00','2019-11-12 00:00:00'],dtype='object',name='datetime')

当我尝试通过merged=pd.merge(df1,df2,left_on=['datetime'],right_on=['datetime'],how='left')合并两个数据帧时,收到消息ValueError: You are trying to merge on datetime64[ns] and object columns. If you wish to proceed you should use pd.concat

也允许我对这两个数据框有所了解。

temperatures = [c for c in df1 if c.startswith('temp')]
df1['temp_mean']=df1[temperatures].mean(axis=1)

In [6]: df1.head(3)
Out[6]:
                    location  temperature1  temperature2  wind  rain  temp_mean
datetime                                           
2019-10-03 00:00:00       HK        18.72          18.78    SW   0.0      18.75
2019-10-03 01:00:00       HK        18.63          18.67    SW   0.1      18.65
2019-10-03 02:00:00       HK        18.29          18.31    SW   0.3      18.30

In [7]:df2
Out[7]: 
                       values
datetime                     
2019-11-03 00:00:00  0.154286
2019-11-04 00:00:00 -5.094286
2019-11-05 00:00:00  1.432857
2019-11-06 00:00:00  0.227143
2019-11-07 00:00:00  0.160000
2019-11-08 00:00:00  1.300000
2019-11-09 00:00:00  0.308571
2019-11-10 00:00:00  0.442857
2019-11-11 00:00:00  0.241429
2019-11-12 00:00:00       NaN

显然,通过合并两个数据框,我希望df2的列“值”将在末尾加入df1,并且任何时候!= '00:00:00'都将被NaN填充,并且这些值将放置在时间== '00:00:00'。由于出现错误并建议使用pd.concat(),因此我输入concated=pd.concat([df1,df2],axis=1,join='outer',ignore_index=False),然后得到下面的输出,其中“值”列在那里,但完全为空(在任何时候我都得到NaN)。>

In [17]: concated.head(3)
Out[17]:
                    location  temperature1  temperature2  wind  rain  temp_mean  \
datetime                                           
2019-10-03 00:00:00       HK        18.72          18.78    SW   0.0      18.75
2019-10-03 01:00:00       HK        18.63          18.67    SW   0.1      18.65
2019-10-03 02:00:00       HK        18.29          18.31    SW   0.3      18.30

                      values
datetime                                           
2019-10-03 00:00:00      NaN
2019-10-03 01:00:00      NaN
2019-10-03 02:00:00      NaN

我不明白自己在这里做错了什么以及如何进行这项工作。

在一开始,我不明白为什么pd.merge()无法与我的数据帧一起使用,然后我不明白为什么pd.concat()无法看到这些值。

这时您的帮助将很有价值,因此,谢谢您。

cycannon 回答:pd.concat()和pd.merge()之间的区别以及为什么我得到错误的输出?

我相信您需要mergeleft_index=Trueright_index=True,因为在两个DatetimeIndex中都由DataFrame进行匹配:

#convert to DatetimeIndex
df2.index = pd.to_datetime(df2.index)
df = pd.merge(df1,df2,left_index=True,right_index=True)
,

您正在尝试合并具有不同数据类型的datetime列。

df1:dtype ='datetime64 [ns]'

df2:dtype ='object'

解决方案:使用以下方法将任一数据类型转换为其他数据类型, .dt.strftime(转换为字符串) 要么 pd.to_datetime(转换为datetime数据类型)

本文链接:https://www.f2er.com/3082527.html

大家都在问