根据df2中的datetimeindex和df2中的datetime列,我有两个想要组合在一起的数据,以便从df1添加column到df1。下面的代码工作正常,但我一直收到一个需要修复的错误。
df1:
datetimeindex name val
2014-01-01 X 0.9
2014-02-01 Y 0.91
2014-03-01 Z 0.92df2:
index datetime SLR
1 2013-10-01 1
2 2013-11-01 2
3 2013-12-01 3我试图通过df2中的datetime列和df1中的datetimeindex将df1与df2结合起来:
df1['SLR'] = df1['date'].map(df2.set_index('date')['SLR'])这是可行的,但我一直收到这个错误:
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
dryhobfr['SLR'] = dryhobfr['date'].map(SLR.set_index('date')['SLR'])
<ipython-input-140-9fa4b21486e9>:17: SettingWithCopyWarning:
...
Try using .loc[row_indexer,col_indexer] = value instead
See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
wethobfr['SLR'] = wethobfr['date'].map(SLR.set_index('date')['SLR'])发布于 2022-07-29 22:40:36
这是另一种方法。你的两个df没有一个共同的日期,所以我不能把结果放在这里
df.merge(df2, how='left',
left_on='datetimeindex',
right_on='datetime').drop(columns=['index','datetime'])我修改了df2的第一行以匹配df1,下面是结果集
datetimeindex name val SLR
0 2014-01-01 X 0.90 1.0
1 2014-02-01 Y 0.91 NaN
2 2014-03-01 Z 0.92 NaNhttps://stackoverflow.com/questions/73171441
复制相似问题