python笔记
今天过得怎么样?梦想是不是更远了?
目录
python字典
python字典的内置方法
dict.has_key(key)
如果键在字典dict里返回true,否则返回falsedict.items()
以列表返回可遍历的(键, 值) 元组数组dict.keys()
以列表返回一个字典所有的键dict.values()
以列表返回字典中的所有值dict.get(key, default=None)
返回指定键的值,如果值不在字典中返回default值dict.setdefault(key, default=None)
和get()类似, 但如果键不存在于字典中,将会添加键并将值设为defaultdict.copy()
返回一个字典的浅复制dict.clear()
删除字典内所有元素dict.fromkeys(seq[, val]))
创建一个新字典,以序列 seq 中元素做字典的键,val 为字典所有键对应的初始值dict.update(dict2)
把字典dict2的键/值对更新到dict里
matplotlib
python-matplotlib画图添加标签:
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(-1,1,10)
y=np.cos(x**2)
fig=plt.figure(figsize=(8,4))
ax=plt.subplot(111)
plt.plot(x,y)
for i,(_x,_y) in enumerate(zip(x,y)):
plt.text(_x,_y,i,color='red',fontsize=i+10)#按照画图坐标轴添加文本
plt.text(0.5,0.8,'subplot words',color='blue',ha='center',transform=ax.transAxes)
plt.figtext(0.5,0.2,'figurewords',color='green',ha='center',transform=ax.transAxes)#按照画布比例坐标轴添加文本
plt.annotate('top',xy=0,1),xytext=0.2,0.8),arrowprops=dict(facecolor='blue',shrink=0.05))#按照画图坐标轴添加文本
plt.show()
pandas
缺失数据补齐
df.fillna(method=’bfill’/’ffill’,axis=)沿0或者1轴向前或者向后补齐
In [152]: df1 = pd.DataFrame({'a':[1,2,3,np.nan],
'b':[np.nan,2,3,4],
'c':[1,np.nan,5,6]})
In [153]: df1
Out[153]:
a b c
0 1.0 NaN 1.0
1 2.0 2.0 NaN
2 3.0 3.0 5.0
3 NaN 4.0 6.0
In [155]: df1.fillna(method='bfill',axis=0)
Out[155]:
a b c
0 1.0 2.0 1.0
1 2.0 2.0 5.0
2 3.0 3.0 5.0
3 NaN 4.0 6.0
In [156]: df1.fillna(method='ffill',axis=1)
Out[156]:
a b c
0 1.0 1.0 1.0
1 2.0 2.0 2.0
2 3.0 3.0 5.0
3 NaN 4.0 6.0
df1.interpolate(method= ,axis= )按照缺失位置前后的数据计算缺失位置的值
method : {‘linear’, ‘time’, ‘index’, ‘values’, ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘barycentric’, ‘krogh’, ‘polynomial’, ‘spline’, ‘piecewise_polynomial’, ‘from_derivatives’, ‘pchip’, ‘akima’}
1. * ‘linear’: ignore the index and treat the values as equally spaced. This is the only method supported on MultiIndexes. default
2. * ‘time’: interpolation works on daily and higher resolution data to interpolate given length of interval
3. * ‘index’, ‘values’: use the actual numerical values of the index
4. * ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’,’barycentric’, ‘polynomial’ is passed toscipy.interpolate.interp1d
. Both ‘polynomial’ and ‘spline’ require that you also specify anorder
(int), e.g. df.interpolate(method=’polynomial’, order=4). These use the actual numerical values of the index.
5. * ‘krogh’, ‘piecewise_polynomial’, ‘spline’, ‘pchip’ and ‘akima’ are all wrappers around the scipy interpolation methods of similar names. These use the actual numerical values of the index. See the scipy documentation for more on their behavior
here <http://docs.scipy.org/doc/scipy/reference/interpolate.html#univariate-interpolation>
__ # noqa
and here <http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html>
__ # noqa
6. * ‘from_derivatives’ refers to BPoly.from_derivatives which replaces ‘piecewise_polynomial’ interpolation method in scipy 0.18
In [65]: df = pd.DataFrame({'A': [1, 2.1, np.nan, 4.7, 5.6, 6.8],
....: 'B': [.25, np.nan, np.nan, 4, 12.2, 14.4]})
....:
In [66]: df
Out[66]:
A B
0 1.0 0.25
1 2.1 NaN
2 NaN NaN
3 4.7 4.00
4 5.6 12.20
5 6.8 14.40
In [67]: df.interpolate()
Out[67]:
A B
0 1.0 0.25
1 2.1 1.50
2 3.4 2.75
3 4.7 4.00
4 5.6 12.20
5 6.8 14.40
In [160]: df.interpolate().plot()
Out[160]: <matplotlib.axes._subplots.AxesSubplot at 0x22803444940>