问题导读:
1.pandas 的数据结构介绍
2. 利用 pandas 对数据的基本操作
3. 汇总和计算描述统计方法
4. 处理缺失数据
5. 层次化索引
解决方案:
- In[6] : obj = pd.Series([1, 2, 3, 4]) In[7] : obj Out[7] : 0 1 1 2 2 3 3 4 dtype: int64
- In[8] : obj.values Out[8] : array([1, 2, 3, 4]) In[9] : obj.index Out[9] : Int64Index([0, 1, 2, 3], dtype = 'int64')
- In[10] : obj2 = pd.Series([2, 4, 1, 6], index = ['a', 'b', 'c', 'd']) In[11] : obj2 Out[11] : a 2 b 4 c 1 d 6 dtype: int64 In[12] : obj2.c Out[12] : 1
- In[18] : obj2[obj2 > 0] Out[18] : a 2 b 4 c 1 d 6 dtype: int64 In[19] : obj2 * 2 Out[19] : a 4 b 8 c 2 d 12 dtype: int64 In[20] : np.exp(obj2) Out[20] : a 7.389056 b 54.598150 c 2.718282 d 403.428793 dtype: float64
- In[21] : sdata = {
- 'Ohio': 2000,
- 'Texas': 3000,
- 'Utah': 3425,
- 'Oregon': 3908
- }
- In[22] : obj3 = pd.Series(sdata) In[23] : obj3 Out[23] : Ohio 2000 Oregon 3908 Texas 3000 Utah 3425 dtype: int64 In[24] : obj3.values Out[24] : array([2000, 3908, 3000, 3425]) In[25] : obj3.index Out[25] : Index([u 'Ohio', u 'Oregon', u 'Texas', u 'Utah'], dtype = 'object')
- n[26] : states = ['california', 'Ohio', 'Oregon', 'Texas'] In[27] : obj4 = pd.Series(sdata, index = states) In[28] : obj4 Out[28] : california NaN Ohio 2000 Oregon 3908 Texas 3000 dtype: float64
- In[34] : obj4.isnull() Out[34] : california True Ohio False Oregon False Texas False dtype: bool In[35] : obj4.notnull() Out[35] : california False Ohio True Oregon True Texas True dtype: bool
- In[36] : obj3 Out[36] : Ohio 2000 Oregon 3908 Texas 3000 Utah 3425 dtype: int64 In[37] : obj4 Out[37] : california NaN Ohio 2000 Oregon 3908 Texas 3000 dtype: float64 In[38] : obj3 + obj4 Out[38] : Ohio 4000 Oregon 7816 Texas 6000 Utah NaN california NaN dtype: float64
- In[40] : obj4.name = 'population'In[41] : obj4.index.name = 'state'In[42] : obj4 Out[42] : state california NaN Ohio 2000 Oregon 3908 Texas 3000 Name: population,
- dtype: float64 In[43] : obj Out[43] : 0 1 1 2 2 3 3 4 dtype: int64 In[45] : obj.index = ['a', 'b', 'c', 'd'] In[46] : obj Out[46] : a 1 b 2 c 3 d 4 dtype: int64
来源: http://lib.csdn.net/article/python/47305