这是我最近的一个问题。感谢Bob Haffner我几乎得到了一个解决方案,可以使用另一个df中的值来更新一个df中的列。
我有df1:
shares
trans_date symbol
2011-01-13 AAPL -1500
IBM 4000
2011-01-26 GOOG 1000
2011-02-02 XOM -4000
2011-02-10 XOM 4000
2011-03-03 GOOG -1000
IBM -2200
2011-05-03 IBM 1500
2011-06-03 IBM -3300
2011-06-10 AAPL 1200
2011-08-01 GOOG 0
2011-12-20 AAPL -1200
df1 info:
<class 'pandas.core.frame.DataFrame'>
MultiIndex: 12 entries, (2011-01-13 00:00:00, AAPL) to (2011-12-20 00:00:00, AAPL)
Data columns (total 1 columns):
shares 12 non-null int64
dtypes: int64(1)
memory usage: 192.0+ bytes
这个df2:
2011-01-13 GOOG 0
AAPL 0
XOM 0
IBM 0
_CASH 0
GOOG 0
AAPL 0
XOM 0
IBM 0
_CASH 0
2011-01-26 GOOG 0
AAPL 0
XOM 0
IBM 0
_CASH 0
2011-02-02 GOOG 0
AAPL 0
XOM 0
IBM 0
_CASH 0
2011-02-10 GOOG 0
AAPL 0
XOM 0
IBM 0
_CASH 0
2011-03-03 GOOG 0
AAPL 0
XOM 0
IBM 0
_CASH 0
..
df2信息是:
return pd.TimeSeries(index=dates, data=dates)
<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.frame.DataFrame'>
MultiIndex: 65 entries, (2011-01-13, GOOG) to (2011-12-20, _CASH)
Data columns (total 1 columns):
0 65 non-null int64
dtypes: int64(1)
memory usage: 1.0+ KB
None
然后我发出一个df.update命令:
df2_updated = df2.update(df1)
这将返回“无”#39;。我无法理解为什么指数不匹配。我很感激任何帮助。
Drew Yallop