这几天把pandas重温了一下,结合聚宽数据,感觉更好理解聚宽的用法。
import pandas as pd import numpy as npimport matplotlib.pyplot as plt
一直没有系统的学习过pandas,正好看到一篇文章,结合聚宽再捋一遍。
guojia = ['usa','ru','cn','jp']qty = [100,300,500,200]
ser = pd.Series(qty,guojia)
ser
usa 100 ru 300 cn 500 jp 200 dtype: int64
ser2 = pd.Series(guojia,qty)
ser2
100 usa 300 ru 500 cn 200 jp dtype: object
ser
usa 100 ru 300 cn 500 jp 200 dtype: int64
ser['ru']
300
ser['jp']
200
ser = pd.Series(qty,guojia)
guojia2 =['au','cn','jp','usa']qty2 =[200,300,400,598]
ser3 = pd.Series(qty2,guojia2)
ser3
au 200 cn 300 jp 400 usa 598 dtype: int64
ser - ser3
au NaN cn 200.0 jp -200.0 ru NaN usa -498.0 dtype: float64
ser + ser3
au NaN cn 800.0 jp 600.0 ru NaN usa 698.0 dtype: float64
df = get_price('RB9999.XSGE',start_date='2018-12-1', end_date='2019-3-1',fields=None)
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | |
---|---|---|---|---|---|---|
2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 |
2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 |
2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 |
2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 |
2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 |
df['open'].head()
2018-12-03 3250.0 2018-12-04 3306.0 2018-12-05 3389.0 2018-12-06 3470.0 2018-12-07 3360.0 Name: open, dtype: float64
df['volume'].tail()
2019-02-25 3659076.0 2019-02-26 3998202.0 2019-02-27 2948082.0 2019-02-28 3461158.0 2019-03-01 2986416.0 Name: volume, dtype: float64
df['open'].head()
2018-12-03 3250.0 2018-12-04 3306.0 2018-12-05 3389.0 2018-12-06 3470.0 2018-12-07 3360.0 Name: open, dtype: float64
type(df['open'])
pandas.core.series.Series
df2 =df[['open','volume']].head()
df2
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | volume | |
---|---|---|
2018-12-03 | 3250.0 | 5092070.0 |
2018-12-04 | 3306.0 | 4807696.0 |
2018-12-05 | 3389.0 | 5400890.0 |
2018-12-06 | 3470.0 | 4500922.0 |
2018-12-07 | 3360.0 | 5127774.0 |
type(df2)
pandas.core.frame.DataFrame
df['o-c'] =pd.Series()
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | o-c | |
---|---|---|---|---|---|---|---|
2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN |
2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN |
2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN |
2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN |
2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN |
df['o-c2'] = df['open'] - df['close']
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | o-c | o-c2 | |
---|---|---|---|---|---|---|---|---|
2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 |
2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 |
2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 |
2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 |
2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 |
def cmp(df1,df2):cmp = df1.values - df2.values# if cmp > 0:# cmp1 = True# else:# cmp1 = Falsereturn cmp
df['cmp'] = cmp(df['open'],df['close'])
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | cmp | |
---|---|---|---|---|---|---|---|
2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | -83.0 |
2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | -68.0 |
2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | -74.0 |
2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | 95.0 |
2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | -37.0 |
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | o-c | o-c2 | |
---|---|---|---|---|---|---|---|---|
2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 |
2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 |
2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 |
2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 |
2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 |
df.head().drop('o-c',axis=1)
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | o-c2 | |
---|---|---|---|---|---|---|---|
2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | -83.0 |
2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | -68.0 |
2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | -74.0 |
2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | 95.0 |
2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | -37.0 |
请务必记住,除非用户明确指定,否则在调用 .drop() 的时候,Pandas 并不会真的永久性地删除这行/列。这主要是为了防止用户误操作丢失数据。
你可以通过调用 df 来确认数据的完整性。如果你确定要永久性删除某一行/列,你需要加上 inplace=True 参数,比如:
重新打印df
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | o-c | o-c2 | |
---|---|---|---|---|---|---|---|---|
2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 |
2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 |
2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 |
2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 |
2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 |
永久删除 inplace =True .....(报错了,以后再学)
df.head().drop('o-c',axis=1,inplace=True )
/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py:3697: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame See the c*eats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy errors=errors)
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | o-c | o-c2 | |
---|---|---|---|---|---|---|---|---|
2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 |
2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 |
2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 |
2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 |
2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 |
df.head().loc['2018-12-05']
open 3.389000e+03 close 3.463000e+03 high 3.466000e+03 low 3.362000e+03 volume 5.400890e+06 money 1.840964e+11 o-c NaN o-c2 -7.400000e+01 Name: 2018-12-05 00:00:00, dtype: float64
type(df.head().loc['2018-12-07'])
pandas.core.series.Series
df.head(10).iloc[[1,9]]
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | o-c | o-c2 | |
---|---|---|---|---|---|---|---|---|
2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 |
2018-12-14 | 3416.0 | 3445.0 | 3450.0 | 3405.0 | 4124206.0 | 1.413454e+11 | NaN | -29.0 |
df.head().loc['2018-12-07',['money']]
money 1.729243e+11 Name: 2018-12-07 00:00:00, dtype: float64
df.head().loc['2018-12-07',['money','high','open']]
money 1.729243e+11 high 3.426000e+03 open 3.360000e+03 Name: 2018-12-07 00:00:00, dtype: float64
type(df.head().loc['2018-12-05',['open','high','volume']])
pandas.core.series.Series
df.head(10).iloc[['2018-12-04','2018-12-14']]
-ValueError Traceback (most recent call last)<ipython-input-73-74b1efb5eb04> in <module>> 1 df.head(10).iloc[['2018-12-04','2018-12-14']]/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py in __getitem__(self, key) 1476 1477 maybe_callable = com._apply_if_callable(key, self.obj)-> 1478 return self._getitem_axis(maybe_callable, axis=axis) 1479 1480 def _is_scalar_access(self, key):/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis) 2089 # a list of integers 2090 elif is_list_like_indexer(key):-> 2091 return self._get_list_axis(key, axis=axis) 2092 2093 # a single integer/opt/conda/lib/python3.6/site-packages/pandas/core/indexing.py in _get_list_axis(self, key, axis) 2068 axis = self.axis or 0 2069 try:-> 2070 return self.obj._take(key, axis=axis) 2071 except IndexError: 2072 # re-raise with different error message/opt/conda/lib/python3.6/site-packages/pandas/core/generic.py in _take(self, indices, axis, is_copy) 2787 new_data = self._data.take(indices, 2788 axis=self._get_block_manager_axis(axis),-> 2789 verify=True) 2790 result = self._constructor(new_data).__finalize__(self) 2791 /opt/conda/lib/python3.6/site-packages/pandas/core/internals.py in take(self, indexer, axis, verify, convert) 4524 dtype='int64') 4525 if isinstance(indexer, slice)-> 4526 else np.asanyarray(indexer, dtype='int64')) 4527 4528 n = self.shape[axis]/opt/conda/lib/python3.6/site-packages/numpy/core/numeric.py in asanyarray(a, dtype, order) 542 543 """> 544 return array(a, dtype, copy=False, order=order, subok=True) 545 546 ValueError: invalid literal for int() with base 10: '2018-12-04'
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | o-c | o-c2 | |
---|---|---|---|---|---|---|---|---|
2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 |
2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 |
2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 |
2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 |
2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 |
df['o-c2']>0
2018-12-03 False 2018-12-04 False 2018-12-05 False 2018-12-06 True 2018-12-07 False 2018-12-10 True 2018-12-11 False 2018-12-12 True 2018-12-13 False 2018-12-14 False 2018-12-17 True 2018-12-18 False 2018-12-19 True 2018-12-20 False 2018-12-21 False 2018-12-24 True 2018-12-25 True 2018-12-26 False 2018-12-27 True 2018-12-28 False 2019-01-02 True 2019-01-03 False 2019-01-04 False 2019-01-07 False 2019-01-08 True 2019-01-09 False 2019-01-10 False 2019-01-11 False 2019-01-14 False 2019-01-15 True 2019-01-16 False 2019-01-17 False 2019-01-18 False 2019-01-21 False 2019-01-22 True 2019-01-23 False 2019-01-24 False 2019-01-25 False 2019-01-28 True 2019-01-29 True 2019-01-30 True 2019-01-31 False 2019-02-01 False 2019-02-11 True 2019-02-12 True 2019-02-13 True 2019-02-14 True 2019-02-15 True 2019-02-18 False 2019-02-19 True 2019-02-20 True 2019-02-21 False 2019-02-22 False 2019-02-25 True 2019-02-26 False 2019-02-27 True 2019-02-28 False 2019-03-01 False Name: o-c2, dtype: bool
df[df['o-c2']>0][['open','close','money']]
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | money | |
---|---|---|---|
2018-12-06 | 3470.0 | 3375.0 | 1.535990e+11 |
2018-12-10 | 3388.0 | 3312.0 | 1.584575e+11 |
2018-12-12 | 3340.0 | 3317.0 | 1.404878e+11 |
2018-12-17 | 3448.0 | 3435.0 | 1.345986e+11 |
2018-12-19 | 3432.0 | 3425.0 | 1.328182e+11 |
2018-12-24 | 3515.0 | 3451.0 | 1.227598e+11 |
2018-12-25 | 3442.0 | 3398.0 | 1.473822e+11 |
2018-12-27 | 3418.0 | 3396.0 | 1.428099e+11 |
2019-01-02 | 3398.0 | 3382.0 | 7.286511e+10 |
2019-01-08 | 3521.0 | 3505.0 | 9.602977e+10 |
2019-01-15 | 3576.0 | 3519.0 | 1.105785e+11 |
2019-01-22 | 3650.0 | 3633.0 | 1.455658e+11 |
2019-01-28 | 3712.0 | 3681.0 | 1.238699e+11 |
2019-01-29 | 3680.0 | 3675.0 | 9.212779e+10 |
2019-01-30 | 3683.0 | 3677.0 | 1.671236e+11 |
2019-02-11 | 3850.0 | 3825.0 | 1.342309e+11 |
2019-02-12 | 3818.0 | 3785.0 | 1.180507e+11 |
2019-02-13 | 3780.0 | 3702.0 | 1.425991e+11 |
2019-02-14 | 3711.0 | 3684.0 | 1.205294e+11 |
2019-02-15 | 3680.0 | 3599.0 | 1.423635e+11 |
2019-02-19 | 3670.0 | 3655.0 | 1.013994e+11 |
2019-02-20 | 3650.0 | 3641.0 | 1.418150e+11 |
2019-02-25 | 3736.0 | 3682.0 | 1.363059e+11 |
2019-02-27 | 3737.0 | 3715.0 | 1.098979e+11 |
你可以用逻辑运算符 &(与)和 |(或)来链接多个条件语句,以便一次应用多个筛选条件到当前的 DataFrame 上。举个栗子,你可以用下面的方法筛选出同时满足 'o-c2'>0 和'close'> 3400 的行:比如 ‘2018-12-06’这行
df.head(20)
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | o-c | o-c2 | |
---|---|---|---|---|---|---|---|---|
2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 |
2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 |
2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 |
2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 |
2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 |
2018-12-10 | 3388.0 | 3312.0 | 3398.0 | 3303.0 | 4741366.0 | 1.584575e+11 | NaN | 76.0 |
2018-12-11 | 3306.0 | 3327.0 | 3341.0 | 3284.0 | 3595984.0 | 1.189109e+11 | NaN | -21.0 |
2018-12-12 | 3340.0 | 3317.0 | 3379.0 | 3317.0 | 4194776.0 | 1.404878e+11 | NaN | 23.0 |
2018-12-13 | 3325.0 | 3419.0 | 3431.0 | 3321.0 | 5327258.0 | 1.805737e+11 | NaN | -94.0 |
2018-12-14 | 3416.0 | 3445.0 | 3450.0 | 3405.0 | 4124206.0 | 1.413454e+11 | NaN | -29.0 |
2018-12-17 | 3448.0 | 3435.0 | 3470.0 | 3414.0 | 3915682.0 | 1.345986e+11 | NaN | 13.0 |
2018-12-18 | 3430.0 | 3435.0 | 3443.0 | 3391.0 | 3671214.0 | 1.255123e+11 | NaN | -5.0 |
2018-12-19 | 3432.0 | 3425.0 | 3450.0 | 3411.0 | 3871012.0 | 1.328182e+11 | NaN | 7.0 |
2018-12-20 | 3433.0 | 3481.0 | 3492.0 | 3427.0 | 4306708.0 | 1.488089e+11 | NaN | -48.0 |
2018-12-21 | 3500.0 | 3508.0 | 3535.0 | 3471.0 | 4313258.0 | 1.511073e+11 | NaN | -8.0 |
2018-12-24 | 3515.0 | 3451.0 | 3516.0 | 3435.0 | 3542690.0 | 1.227598e+11 | NaN | 64.0 |
2018-12-25 | 3442.0 | 3398.0 | 3448.0 | 3357.0 | 4333400.0 | 1.473822e+11 | NaN | 44.0 |
2018-12-26 | 3393.0 | 3409.0 | 3430.0 | 3387.0 | 3285056.0 | 1.119878e+11 | NaN | -16.0 |
2018-12-27 | 3418.0 | 3396.0 | 3471.0 | 3395.0 | 4172568.0 | 1.428099e+11 | NaN | 22.0 |
2018-12-28 | 3387.0 | 3404.0 | 3420.0 | 3382.0 | 2915936.0 | 9.916103e+10 | NaN | -17.0 |
df[ (df['o-c2']>0) & (df['close'] > 3400) ]
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | o-c | o-c2 | |
---|---|---|---|---|---|---|---|---|
2018-12-17 | 3448.0 | 3435.0 | 3470.0 | 3414.0 | 3915682.0 | 1.345986e+11 | NaN | 13.0 |
2018-12-19 | 3432.0 | 3425.0 | 3450.0 | 3411.0 | 3871012.0 | 1.328182e+11 | NaN | 7.0 |
2018-12-24 | 3515.0 | 3451.0 | 3516.0 | 3435.0 | 3542690.0 | 1.227598e+11 | NaN | 64.0 |
2019-01-08 | 3521.0 | 3505.0 | 3523.0 | 3483.0 | 2740906.0 | 9.602977e+10 | NaN | 16.0 |
2019-01-15 | 3576.0 | 3519.0 | 3576.0 | 3508.0 | 3128980.0 | 1.105785e+11 | NaN | 57.0 |
2019-01-22 | 3650.0 | 3633.0 | 3700.0 | 3630.0 | 3970252.0 | 1.455658e+11 | NaN | 17.0 |
2019-01-28 | 3712.0 | 3681.0 | 3757.0 | 3680.0 | 3337710.0 | 1.238699e+11 | NaN | 31.0 |
2019-01-29 | 3680.0 | 3675.0 | 3694.0 | 3653.0 | 2508228.0 | 9.212779e+10 | NaN | 5.0 |
2019-01-30 | 3683.0 | 3677.0 | 3767.0 | 3667.0 | 4490378.0 | 1.671236e+11 | NaN | 6.0 |
2019-02-11 | 3850.0 | 3825.0 | 3908.0 | 3816.0 | 3480360.0 | 1.342309e+11 | NaN | 25.0 |
2019-02-12 | 3818.0 | 3785.0 | 3830.0 | 3771.0 | 3109428.0 | 1.180507e+11 | NaN | 33.0 |
2019-02-13 | 3780.0 | 3702.0 | 3796.0 | 3692.0 | 3814494.0 | 1.425991e+11 | NaN | 78.0 |
2019-02-14 | 3711.0 | 3684.0 | 3717.0 | 3668.0 | 3266348.0 | 1.205294e+11 | NaN | 27.0 |
2019-02-15 | 3680.0 | 3599.0 | 3696.0 | 3593.0 | 3916316.0 | 1.423635e+11 | NaN | 81.0 |
2019-02-19 | 3670.0 | 3655.0 | 3698.0 | 3647.0 | 2760590.0 | 1.013994e+11 | NaN | 15.0 |
2019-02-20 | 3650.0 | 3641.0 | 3669.0 | 3578.0 | 3918544.0 | 1.418150e+11 | NaN | 9.0 |
2019-02-25 | 3736.0 | 3682.0 | 3766.0 | 3681.0 | 3659076.0 | 1.363059e+11 | NaN | 54.0 |
2019-02-27 | 3737.0 | 3715.0 | 3750.0 | 3697.0 | 2948082.0 | 1.098979e+11 | NaN | 22.0 |
xx = (df['o-c2']>0)
type(xx)
pandas.core.series.Series
yy = df['o-c2'] >0
type(yy)
pandas.core.series.Series
df[ (df['o-c2'] >0) & (df['close'] > 3400) ]
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | o-c | o-c2 | |
---|---|---|---|---|---|---|---|---|
2018-12-17 | 3448.0 | 3435.0 | 3470.0 | 3414.0 | 3915682.0 | 1.345986e+11 | NaN | 13.0 |
2018-12-19 | 3432.0 | 3425.0 | 3450.0 | 3411.0 | 3871012.0 | 1.328182e+11 | NaN | 7.0 |
2018-12-24 | 3515.0 | 3451.0 | 3516.0 | 3435.0 | 3542690.0 | 1.227598e+11 | NaN | 64.0 |
2019-01-08 | 3521.0 | 3505.0 | 3523.0 | 3483.0 | 2740906.0 | 9.602977e+10 | NaN | 16.0 |
2019-01-15 | 3576.0 | 3519.0 | 3576.0 | 3508.0 | 3128980.0 | 1.105785e+11 | NaN | 57.0 |
2019-01-22 | 3650.0 | 3633.0 | 3700.0 | 3630.0 | 3970252.0 | 1.455658e+11 | NaN | 17.0 |
2019-01-28 | 3712.0 | 3681.0 | 3757.0 | 3680.0 | 3337710.0 | 1.238699e+11 | NaN | 31.0 |
2019-01-29 | 3680.0 | 3675.0 | 3694.0 | 3653.0 | 2508228.0 | 9.212779e+10 | NaN | 5.0 |
2019-01-30 | 3683.0 | 3677.0 | 3767.0 | 3667.0 | 4490378.0 | 1.671236e+11 | NaN | 6.0 |
2019-02-11 | 3850.0 | 3825.0 | 3908.0 | 3816.0 | 3480360.0 | 1.342309e+11 | NaN | 25.0 |
2019-02-12 | 3818.0 | 3785.0 | 3830.0 | 3771.0 | 3109428.0 | 1.180507e+11 | NaN | 33.0 |
2019-02-13 | 3780.0 | 3702.0 | 3796.0 | 3692.0 | 3814494.0 | 1.425991e+11 | NaN | 78.0 |
2019-02-14 | 3711.0 | 3684.0 | 3717.0 | 3668.0 | 3266348.0 | 1.205294e+11 | NaN | 27.0 |
2019-02-15 | 3680.0 | 3599.0 | 3696.0 | 3593.0 | 3916316.0 | 1.423635e+11 | NaN | 81.0 |
2019-02-19 | 3670.0 | 3655.0 | 3698.0 | 3647.0 | 2760590.0 | 1.013994e+11 | NaN | 15.0 |
2019-02-20 | 3650.0 | 3641.0 | 3669.0 | 3578.0 | 3918544.0 | 1.418150e+11 | NaN | 9.0 |
2019-02-25 | 3736.0 | 3682.0 | 3766.0 | 3681.0 | 3659076.0 | 1.363059e+11 | NaN | 54.0 |
2019-02-27 | 3737.0 | 3715.0 | 3750.0 | 3697.0 | 2948082.0 | 1.098979e+11 | NaN | 22.0 |
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | o-c | o-c2 | |
---|---|---|---|---|---|---|---|---|
2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 |
2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 |
2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 |
2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 |
2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 |
df.reset_index().head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
index | open | close | high | low | volume | money | o-c | o-c2 | |
---|---|---|---|---|---|---|---|---|---|
0 | 2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 |
1 | 2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 |
2 | 2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 |
3 | 2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 |
4 | 2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 |
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | o-c | o-c2 | |
---|---|---|---|---|---|---|---|---|
2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 |
2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 |
2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 |
2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 |
2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 |
df.reset_index(inplace=True)
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
index | open | close | high | low | volume | money | o-c | o-c2 | |
---|---|---|---|---|---|---|---|---|---|
0 | 2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 |
1 | 2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 |
2 | 2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 |
3 | 2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 |
4 | 2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 |
:
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
index | open | close | high | low | volume | money | o-c | o-c2 | |
---|---|---|---|---|---|---|---|---|---|
0 | 2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 |
1 | 2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 |
2 | 2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 |
3 | 2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 |
4 | 2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 |
df['code'] ='rb9999'
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
index | open | close | high | low | volume | money | o-c | o-c2 | code | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 | rb9999 |
1 | 2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 | rb9999 |
2 | 2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 | rb9999 |
3 | 2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 | rb9999 |
4 | 2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 | rb9999 |
df.set_index('code').head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
index | open | close | high | low | volume | money | o-c | o-c2 | |
---|---|---|---|---|---|---|---|---|---|
code | |||||||||
rb9999 | 2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 |
rb9999 | 2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 |
rb9999 | 2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 |
rb9999 | 2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 |
rb9999 | 2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 |
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
index | open | close | high | low | volume | money | o-c | o-c2 | code | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 | rb9999 |
1 | 2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 | rb9999 |
2 | 2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 | rb9999 |
3 | 2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 | rb9999 |
4 | 2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 | rb9999 |
注意,不像 .reset_index() 会保留一个备份,然后才用默认的索引值代替原索引,.set_index() 将会完全覆盖原来的索引值。
应该暂时用不到多级索引,以后学习。
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
index | open | close | high | low | volume | money | o-c | o-c2 | code | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 | rb9999 |
1 | 2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 | rb9999 |
2 | 2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 | rb9999 |
3 | 2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 | rb9999 |
4 | 2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 | rb9999 |
我们可以用 .index.names 给它们加上名字????
df.head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
index | open | close | high | low | volume | money | o-c | o-c2 | code | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 | rb9999 |
1 | 2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 | rb9999 |
2 | 2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 | rb9999 |
3 | 2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 | rb9999 |
4 | 2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 | rb9999 |
df.dropna()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
index | open | close | high | low | volume | money | o-c | o-c2 | code |
---|
df.dropna(axis = 1).head()
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
index | open | close | high | low | volume | money | o-c2 | code | |
---|---|---|---|---|---|---|---|---|---|
0 | 2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | -83.0 | rb9999 |
1 | 2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | -68.0 | rb9999 |
2 | 2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | -74.0 | rb9999 |
3 | 2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | 95.0 | rb9999 |
4 | 2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | -37.0 | rb9999 |
df
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
index | open | close | high | low | volume | money | o-c | o-c2 | code | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | NaN | -83.0 | rb9999 |
1 | 2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | NaN | -68.0 | rb9999 |
2 | 2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | NaN | -74.0 | rb9999 |
3 | 2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | NaN | 95.0 | rb9999 |
4 | 2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | NaN | -37.0 | rb9999 |
5 | 2018-12-10 | 3388.0 | 3312.0 | 3398.0 | 3303.0 | 4741366.0 | 1.584575e+11 | NaN | 76.0 | rb9999 |
6 | 2018-12-11 | 3306.0 | 3327.0 | 3341.0 | 3284.0 | 3595984.0 | 1.189109e+11 | NaN | -21.0 | rb9999 |
7 | 2018-12-12 | 3340.0 | 3317.0 | 3379.0 | 3317.0 | 4194776.0 | 1.404878e+11 | NaN | 23.0 | rb9999 |
8 | 2018-12-13 | 3325.0 | 3419.0 | 3431.0 | 3321.0 | 5327258.0 | 1.805737e+11 | NaN | -94.0 | rb9999 |
9 | 2018-12-14 | 3416.0 | 3445.0 | 3450.0 | 3405.0 | 4124206.0 | 1.413454e+11 | NaN | -29.0 | rb9999 |
10 | 2018-12-17 | 3448.0 | 3435.0 | 3470.0 | 3414.0 | 3915682.0 | 1.345986e+11 | NaN | 13.0 | rb9999 |
11 | 2018-12-18 | 3430.0 | 3435.0 | 3443.0 | 3391.0 | 3671214.0 | 1.255123e+11 | NaN | -5.0 | rb9999 |
12 | 2018-12-19 | 3432.0 | 3425.0 | 3450.0 | 3411.0 | 3871012.0 | 1.328182e+11 | NaN | 7.0 | rb9999 |
13 | 2018-12-20 | 3433.0 | 3481.0 | 3492.0 | 3427.0 | 4306708.0 | 1.488089e+11 | NaN | -48.0 | rb9999 |
14 | 2018-12-21 | 3500.0 | 3508.0 | 3535.0 | 3471.0 | 4313258.0 | 1.511073e+11 | NaN | -8.0 | rb9999 |
15 | 2018-12-24 | 3515.0 | 3451.0 | 3516.0 | 3435.0 | 3542690.0 | 1.227598e+11 | NaN | 64.0 | rb9999 |
16 | 2018-12-25 | 3442.0 | 3398.0 | 3448.0 | 3357.0 | 4333400.0 | 1.473822e+11 | NaN | 44.0 | rb9999 |
17 | 2018-12-26 | 3393.0 | 3409.0 | 3430.0 | 3387.0 | 3285056.0 | 1.119878e+11 | NaN | -16.0 | rb9999 |
18 | 2018-12-27 | 3418.0 | 3396.0 | 3471.0 | 3395.0 | 4172568.0 | 1.428099e+11 | NaN | 22.0 | rb9999 |
19 | 2018-12-28 | 3387.0 | 3404.0 | 3420.0 | 3382.0 | 2915936.0 | 9.916103e+10 | NaN | -17.0 | rb9999 |
20 | 2019-01-02 | 3398.0 | 3382.0 | 3439.0 | 3371.0 | 2149936.0 | 7.286511e+10 | NaN | 16.0 | rb9999 |
21 | 2019-01-03 | 3378.0 | 3455.0 | 3456.0 | 3366.0 | 3719028.0 | 1.271959e+11 | NaN | -77.0 | rb9999 |
22 | 2019-01-04 | 3460.0 | 3486.0 | 3493.0 | 3430.0 | 3323322.0 | 1.149595e+11 | NaN | -26.0 | rb9999 |
23 | 2019-01-07 | 3490.0 | 3520.0 | 3526.0 | 3466.0 | 3053042.0 | 1.068218e+11 | NaN | -30.0 | rb9999 |
24 | 2019-01-08 | 3521.0 | 3505.0 | 3523.0 | 3483.0 | 2740906.0 | 9.602977e+10 | NaN | 16.0 | rb9999 |
25 | 2019-01-09 | 3505.0 | 3507.0 | 3550.0 | 3494.0 | 3378598.0 | 1.190845e+11 | NaN | -2.0 | rb9999 |
26 | 2019-01-10 | 3512.0 | 3514.0 | 3543.0 | 3502.0 | 3134668.0 | 1.104706e+11 | NaN | -2.0 | rb9999 |
27 | 2019-01-11 | 3517.0 | 3539.0 | 3540.0 | 3492.0 | 3440962.0 | 1.210772e+11 | NaN | -22.0 | rb9999 |
28 | 2019-01-14 | 3535.0 | 3575.0 | 3576.0 | 3520.0 | 2624250.0 | 9.324587e+10 | NaN | -40.0 | rb9999 |
29 | 2019-01-15 | 3576.0 | 3519.0 | 3576.0 | 3508.0 | 3128980.0 | 1.105785e+11 | NaN | 57.0 | rb9999 |
30 | 2019-01-16 | 3519.0 | 3534.0 | 3545.0 | 3510.0 | 2632026.0 | 9.282553e+10 | NaN | -15.0 | rb9999 |
31 | 2019-01-17 | 3529.0 | 3551.0 | 3565.0 | 3525.0 | 2258818.0 | 8.021438e+10 | NaN | -22.0 | rb9999 |
32 | 2019-01-18 | 3552.0 | 3633.0 | 3636.0 | 3540.0 | 3599670.0 | 1.294739e+11 | NaN | -81.0 | rb9999 |
33 | 2019-01-21 | 3636.0 | 3645.0 | 3675.0 | 3619.0 | 3526340.0 | 1.285795e+11 | NaN | -9.0 | rb9999 |
34 | 2019-01-22 | 3650.0 | 3633.0 | 3700.0 | 3630.0 | 3970252.0 | 1.455658e+11 | NaN | 17.0 | rb9999 |
35 | 2019-01-23 | 3639.0 | 3644.0 | 3658.0 | 3625.0 | 2631994.0 | 9.588602e+10 | NaN | -5.0 | rb9999 |
36 | 2019-01-24 | 3647.0 | 3680.0 | 3684.0 | 3639.0 | 3200450.0 | 1.171679e+11 | NaN | -33.0 | rb9999 |
37 | 2019-01-25 | 3684.0 | 3710.0 | 3740.0 | 3675.0 | 3099552.0 | 1.150852e+11 | NaN | -26.0 | rb9999 |
38 | 2019-01-28 | 3712.0 | 3681.0 | 3757.0 | 3680.0 | 3337710.0 | 1.238699e+11 | NaN | 31.0 | rb9999 |
39 | 2019-01-29 | 3680.0 | 3675.0 | 3694.0 | 3653.0 | 2508228.0 | 9.212779e+10 | NaN | 5.0 | rb9999 |
40 | 2019-01-30 | 3683.0 | 3677.0 | 3767.0 | 3667.0 | 4490378.0 | 1.671236e+11 | NaN | 6.0 | rb9999 |
41 | 2019-01-31 | 3682.0 | 3707.0 | 3718.0 | 3673.0 | 2724362.0 | 1.006953e+11 | NaN | -25.0 | rb9999 |
42 | 2019-02-01 | 3696.0 | 3754.0 | 3770.0 | 3696.0 | 2506846.0 | 9.364989e+10 | NaN | -58.0 | rb9999 |
43 | 2019-02-11 | 3850.0 | 3825.0 | 3908.0 | 3816.0 | 3480360.0 | 1.342309e+11 | NaN | 25.0 | rb9999 |
44 | 2019-02-12 | 3818.0 | 3785.0 | 3830.0 | 3771.0 | 3109428.0 | 1.180507e+11 | NaN | 33.0 | rb9999 |
45 | 2019-02-13 | 3780.0 | 3702.0 | 3796.0 | 3692.0 | 3814494.0 | 1.425991e+11 | NaN | 78.0 | rb9999 |
46 | 2019-02-14 | 3711.0 | 3684.0 | 3717.0 | 3668.0 | 3266348.0 | 1.205294e+11 | NaN | 27.0 | rb9999 |
47 | 2019-02-15 | 3680.0 | 3599.0 | 3696.0 | 3593.0 | 3916316.0 | 1.423635e+11 | NaN | 81.0 | rb9999 |
48 | 2019-02-18 | 3615.0 | 3659.0 | 3675.0 | 3611.0 | 3532344.0 | 1.287828e+11 | NaN | -44.0 | rb9999 |
49 | 2019-02-19 | 3670.0 | 3655.0 | 3698.0 | 3647.0 | 2760590.0 | 1.013994e+11 | NaN | 15.0 | rb9999 |
50 | 2019-02-20 | 3650.0 | 3641.0 | 3669.0 | 3578.0 | 3918544.0 | 1.418150e+11 | NaN | 9.0 | rb9999 |
51 | 2019-02-21 | 3640.0 | 3677.0 | 3695.0 | 3635.0 | 3928972.0 | 1.442938e+11 | NaN | -37.0 | rb9999 |
52 | 2019-02-22 | 3669.0 | 3731.0 | 3735.0 | 3663.0 | 3278144.0 | 1.215958e+11 | NaN | -62.0 | rb9999 |
53 | 2019-02-25 | 3736.0 | 3682.0 | 3766.0 | 3681.0 | 3659076.0 | 1.363059e+11 | NaN | 54.0 | rb9999 |
54 | 2019-02-26 | 3677.0 | 3736.0 | 3745.0 | 3672.0 | 3998202.0 | 1.482914e+11 | NaN | -59.0 | rb9999 |
55 | 2019-02-27 | 3737.0 | 3715.0 | 3750.0 | 3697.0 | 2948082.0 | 1.098979e+11 | NaN | 22.0 | rb9999 |
56 | 2019-02-28 | 3728.0 | 3750.0 | 3755.0 | 3705.0 | 3461158.0 | 1.292056e+11 | NaN | -22.0 | rb9999 |
57 | 2019-03-01 | 3756.0 | 3815.0 | 3816.0 | 3751.0 | 2986416.0 | 1.127984e+11 | NaN | -59.0 | rb9999 |
类似的,如果你使用 .fillna() 方法,Pandas 将对这个 DataFrame 里所有的空值位置填上你指定的默认值。比如,将表中所有 NaN 替换成 20 :
同理,.dropna() 和 .fillna() 并不会永久性改变你的数据,除非你传入了inplace=True 参数。
df['cross']= nan
df
.dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
open | close | high | low | volume | money | cmp | cross | |
---|---|---|---|---|---|---|---|---|
2018-12-03 | 3250.0 | 3333.0 | 3495.0 | 3208.0 | 5092070.0 | 1.696123e+11 | -83.0 | NaN |
2018-12-04 | 3306.0 | 3374.0 | 3388.0 | 3283.0 | 4807696.0 | 1.600262e+11 | -68.0 | NaN |
2018-12-05 | 3389.0 | 3463.0 | 3466.0 | 3362.0 | 5400890.0 | 1.840964e+11 | -74.0 | NaN |
2018-12-06 | 3470.0 | 3375.0 | 3473.0 | 3371.0 | 4500922.0 | 1.535990e+11 | 95.0 | NaN |
2018-12-07 | 3360.0 | 3397.0 | 3426.0 | 3334.0 | 5127774.0 | 1.729243e+11 | -37.0 | NaN |
2018-12-10 | 3388.0 | 3312.0 | 3398.0 | 3303.0 | 4741366.0 | 1.584575e+11 | 76.0 | NaN |
2018-12-11 | 3306.0 | 3327.0 | 3341.0 | 3284.0 | 3595984.0 | 1.189109e+11 | -21.0 | NaN |
2018-12-12 | 3340.0 | 3317.0 | 3379.0 | 3317.0 | 4194776.0 | 1.404878e+11 | 23.0 | NaN |
2018-12-13 | 3325.0 | 3419.0 | 3431.0 | 3321.0 | 5327258.0 | 1.805737e+11 | -94.0 | NaN |
2018-12-14 | 3416.0 | 3445.0 | 3450.0 | 3405.0 | 4124206.0 | 1.413454e+11 | -29.0 | NaN |
2018-12-17 | 3448.0 | 3435.0 | 3470.0 | 3414.0 | 3915682.0 | 1.345986e+11 | 13.0 | NaN |
2018-12-18 | 3430.0 | 3435.0 | 3443.0 | 3391.0 | 3671214.0 | 1.255123e+11 | -5.0 | NaN |
2018-12-19 | 3432.0 | 3425.0 | 3450.0 | 3411.0 | 3871012.0 | 1.328182e+11 | 7.0 | NaN |
2018-12-20 | 3433.0 | 3481.0 | 3492.0 | 3427.0 | 4306708.0 | 1.488089e+11 | -48.0 | NaN |
2018-12-21 | 3500.0 | 3508.0 | 3535.0 | 3471.0 | 4313258.0 | 1.511073e+11 | -8.0 | NaN |
2018-12-24 | 3515.0 | 3451.0 | 3516.0 | 3435.0 | 3542690.0 | 1.227598e+11 | 64.0 | NaN |
2018-12-25 | 3442.0 | 3398.0 | 3448.0 | 3357.0 | 4333400.0 | 1.473822e+11 | 44.0 | NaN |
2018-12-26 | 3393.0 | 3409.0 | 3430.0 | 3387.0 | 3285056.0 | 1.119878e+11 | -16.0 | NaN |
2018-12-27 | 3418.0 | 3396.0 | 3471.0 | 3395.0 | 4172568.0 | 1.428099e+11 | 22.0 | NaN |
2018-12-28 | 3387.0 | 3404.0 | 3420.0 | 3382.0 | 2915936.0 | 9.916103e+10 | -17.0 | NaN |
2019-01-02 | 3398.0 | 3382.0 | 3439.0 | 3371.0 | 2149936.0 | 7.286511e+10 | 16.0 | NaN |
2019-01-03 | 3378.0 | 3455.0 | 3456.0 | 3366.0 | 3719028.0 | 1.271959e+11 | -77.0 | NaN |
2019-01-04 | 3460.0 | 3486.0 | 3493.0 | 3430.0 | 3323322.0 | 1.149595e+11 | -26.0 | NaN |
2019-01-07 | 3490.0 | 3520.0 | 3526.0 | 3466.0 | 3053042.0 | 1.068218e+11 | -30.0 | NaN |
2019-01-08 | 3521.0 | 3505.0 | 3523.0 | 3483.0 | 2740906.0 | 9.602977e+10 | 16.0 | NaN |
2019-01-09 | 3505.0 | 3507.0 | 3550.0 | 3494.0 | 3378598.0 | 1.190845e+11 | -2.0 | NaN |
2019-01-10 | 3512.0 | 3514.0 | 3543.0 | 3502.0 | 3134668.0 | 1.104706e+11 | -2.0 | NaN |
2019-01-11 | 3517.0 | 3539.0 | 3540.0 | 3492.0 | 3440962.0 | 1.210772e+11 | -22.0 | NaN |
2019-01-14 | 3535.0 | 3575.0 | 3576.0 | 3520.0 | 2624250.0 | 9.324587e+10 | -40.0 | NaN |
2019-01-15 | 3576.0 | 3519.0 | 3576.0 | 3508.0 | 3128980.0 | 1.105785e+11 | 57.0 | NaN |
2019-01-16 | 3519.0 | 3534.0 | 3545.0 | 3510.0 | 2632026.0 | 9.282553e+10 | -15.0 | NaN |
2019-01-17 | 3529.0 | 3551.0 | 3565.0 | 3525.0 | 2258818.0 | 8.021438e+10 | -22.0 | NaN |
2019-01-18 | 3552.0 | 3633.0 | 3636.0 | 3540.0 | 3599670.0 | 1.294739e+11 | -81.0 | NaN |
2019-01-21 | 3636.0 | 3645.0 | 3675.0 | 3619.0 | 3526340.0 | 1.285795e+11 | -9.0 | NaN |
2019-01-22 | 3650.0 | 3633.0 | 3700.0 | 3630.0 | 3970252.0 | 1.455658e+11 | 17.0 | NaN |
2019-01-23 | 3639.0 | 3644.0 | 3658.0 | 3625.0 | 2631994.0 | 9.588602e+10 | -5.0 | NaN |
2019-01-24 | 3647.0 | 3680.0 | 3684.0 | 3639.0 | 3200450.0 | 1.171679e+11 | -33.0 | NaN |
2019-01-25 | 3684.0 | 3710.0 | 3740.0 | 3675.0 | 3099552.0 | 1.150852e+11 | -26.0 | NaN |
2019-01-28 | 3712.0 | 3681.0 | 3757.0 | 3680.0 | 3337710.0 | 1.238699e+11 | 31.0 | NaN |
2019-01-29 | 3680.0 | 3675.0 | 3694.0 | 3653.0 | 2508228.0 | 9.212779e+10 | 5.0 | NaN |
2019-01-30 | 3683.0 | 3677.0 | 3767.0 | 3667.0 | 4490378.0 | 1.671236e+11 | 6.0 | NaN |
2019-01-31 | 3682.0 | 3707.0 | 3718.0 | 3673.0 | 2724362.0 | 1.006953e+11 | -25.0 | NaN |
2019-02-01 | 3696.0 | 3754.0 | 3770.0 | 3696.0 | 2506846.0 | 9.364989e+10 | -58.0 | NaN |
2019-02-11 | 3850.0 | 3825.0 | 3908.0 | 3816.0 | 3480360.0 | 1.342309e+11 | 25.0 | NaN |
2019-02-12 | 3818.0 | 3785.0 | 3830.0 | 3771.0 | 3109428.0 | 1.180507e+11 | 33.0 | NaN |
2019-02-13 | 3780.0 | 3702.0 | 3796.0 | 3692.0 | 3814494.0 | 1.425991e+11 | 78.0 | NaN |
2019-02-14 | 3711.0 | 3684.0 | 3717.0 | 3668.0 | 3266348.0 | 1.205294e+11 | 27.0 | NaN |
2019-02-15 | 3680.0 | 3599.0 | 3696.0 | 3593.0 | 3916316.0 | 1.423635e+11 | 81.0 | NaN |
2019-02-18 | 3615.0 | 3659.0 | 3675.0 | 3611.0 | 3532344.0 | 1.287828e+11 | -44.0 | NaN |
2019-02-19 | 3670.0 | 3655.0 | 3698.0 | 3647.0 | 2760590.0 | 1.013994e+11 | 15.0 | NaN |
2019-02-20 | 3650.0 | 3641.0 | 3669.0 | 3578.0 | 3918544.0 | 1.418150e+11 | 9.0 | NaN |
2019-02-21 | 3640.0 | 3677.0 | 3695.0 | 3635.0 | 3928972.0 | 1.442938e+11 | -37.0 | NaN |
2019-02-22 | 3669.0 | 3731.0 | 3735.0 | 3663.0 | 3278144.0 | 1.215958e+11 | -62.0 | NaN |
2019-02-25 | 3736.0 | 3682.0 | 3766.0 | 3681.0 | 3659076.0 | 1.363059e+11 | 54.0 | NaN |
2019-02-26 | 3677.0 | 3736.0 | 3745.0 | 3672.0 | 3998202.0 | 1.482914e+11 | -59.0 | NaN |
2019-02-27 | 3737.0 | 3715.0 | 3750.0 | 3697.0 | 2948082.0 | 1.098979e+11 | 22.0 | NaN |
2019-02-28 | 3728.0 | 3750.0 | 3755.0 | 3705.0 | 3461158.0 | 1.292056e+11 | -22.0 | NaN |
2019-03-01 | 3756.0 | 3815.0 | 3816.0 | 3751.0 | 2986416.0 | 1.127984e+11 | -59.0 | NaN |
def cross(cmp):if cmp > 0:crs = Trueelse:crs = Falsereturn crs
cross(df['cmp'])
-ValueError Traceback (most recent call last)<ipython-input-26-7833c6976bc2> in <module>> 1 cross(df['cmp'])<ipython-input-25-70d306d78f78> in cross(cmp) 1 def cross(cmp):> 2 if cmp > 0: 3 crs = True 4 else: 5 crs = False/opt/conda/lib/python3.6/site-packages/pandas/core/generic.py in __nonzero__(self) 1574 raise ValueError("The truth value of a {0} is ambiguous. " 1575 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."-> 1576 .format(self.__class__.__name__)) 1577 1578 __bool__ = __nonzero__ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
ts =pd.Series(np.random.randn(1000), index=pd.date_range("2001-1-1",periods = 1000))
df4 = pd.DataFrame(np.random.randn(1000,4),index=ts.index,columns = ['a','b','c','d'])
df5 =df4.cumsum()
df5.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7f774e988400>
df5.dtypes
a float64 b float64 c float64 d float64 dtype: object
df.dtypes
index datetime64[ns] open float64 close float64 high float64 low float64 volume float64 money float64 o-c float64 o-c2 float64 code object dtype: object
本社区仅针对特定人员开放
查看需注册登录并通过风险意识测评
5秒后跳转登录页面...
移动端课程