我从Excel电子表格创建的python中有两个df。我在nflx_analyst_signals df中有一组特定的日期,我只想从nflx_analyst_signals df中的nflx_returns df中获得那些行。我是熊猫的新手,尽管这似乎是一个显而易见的难题,但我似乎无法完成。
这些是我尝试过的以下内容: 1.重置两个df中的索引(reset_index()),并尝试比较日期。 2.将日期列更改为'datetime64 [ns]格式,并尝试比较日期。 3.在nflx_Analysts_signals df中列出日期,并在nflx_returns df中应用np.is
1。
nflx_returns.reset_index()
nflx_analysts_signals.reset_index()
nflx_returns[nflx_returns['date'] == nflx_analysts_signals['dte']]
2
nflx_returns['date'] = nflx_returns['date'].astype('datetime64')
nflx_analysts_signals['dte'] = nflx_analysts_signals['dte'].astype('datetime64')
nflx_returns['CONDN_CHECK'] = nflx_returns[np.where(nflx_returns['date'].equals[nflx_analysts_signals('dte')])]
3。
dates = []
for date in nflx_analysts_signals['dte']:
dates.append(date)
nflx_returns['CONDN'] = np.isin(list(nflx_returns['date']), dates)
在iPython中分别尝试这些不同方法时遇到的错误: 1。
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-11-819bcaeb6291> in <module>
----> 1 nflx_returns[nflx_returns['date'] == nflx_analysts_signals['dte']]
~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\ops.py in wrapper(self, other, axis)
1674
1675 elif isinstance(other, ABCSeries) and not self._indexed_same(other):
-> 1676 raise ValueError("Can only compare identically-labeled "
1677 "Series objects")
1678
ValueError: Can only compare identically-labeled Series objects
2。
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-c94a05234af0> in <module>
----> 1 nflx_returns['CONDN_CHECK'] = nflx_returns[np.where(nflx_returns['date'].equals[nflx_analysts_signals('dte')])]
TypeError: 'DataFrame' object is not callable
3。 该代码有效,但是生成的df中没有行。它是空的基本上,“ CONDN_CHECK”列的所有行均为False
编辑: df的某些行:
date VOLUME DAILY_TOTAL_GROSS_DIVIDEND DAILY_GROSS_DIVIDEND LOG_DAILY_RETURN CUM_TOT_RETURN_5D CUM_TOT_RETURN_20D CUM_TOT_RETURN_60D CUM_TOT_RETURN_120D AVG_VOLUME_5D AVG_VOLUME_20D AVG_VOLUME_60D AVG_VOLUME_120D
2792 2013-06-26 18839520 -0.3758 -0.003758 -0.003765 -0.086996 -0.015046 0.200408 1.195885 20438721.8 21219322.6 2.852531e+07 3.323521e+07
2793 2013-06-27 18332594 1.3531 0.013531 0.013440 -0.038253 -0.034537 0.266465 1.239741 19879419.0 20930923.3 2.757415e+07 3.323990e+07
2794 2013-06-28 19581436 -1.8049 -0.018049 -0.018214 -0.026787 -0.067006 0.266362 1.127918 19116070.4 20638025.8 2.732609e+07 3.302334e+07
2795 2013-07-01 24518284 6.2485 0.062485 0.060611 0.040259 0.010406 0.362078 1.308446 20438378.8 20800170.3 2.719278e+07 3.302161e+07
2796 2013-07-02 17730678 -1.2574 -0.012574 -0.012654 0.040205 -0.017089 0.358149 1.309040 19800502.4 20535986.1 2.695898e+07 3.300082e+07
ticker dte
8 NFLX US 2013-06-28
20 NFLX US 2013-07-31
33 NFLX US 2013-08-30
271 NFLX US 2015-03-31
287 NFLX US 2015-04-30