• Python的Pandas库(一)基础使用


    Python开发实用教程

    Pandas 是基于NumPy 的一种工具,该工具是为解决数据分析任务而创建的。与NumPy十分类似的一点是,NumPy的核心是提供了数组结构,而Pandas 的核心是提供了两种数据结构: Series(一维数据)与 DataFrame(二维数据),特别是DataFrame,可以让开发人员可以像Excel一样灵活、方便的操作二维表格数据。

    基本数据结构

    Series

    Series 是带标签的一维数组,可存储整数、浮点数、字符串、Python 对象等类型的数据。轴标签统称为索引。它与此前学习的命名元组(collections.namedtuple)十分的相似。

    Series的创建

    调用 pd.Series 函数即可创建 Series:

    import pandas as pd

    s=pd.Series( data, index, dtype, copy)

    data 支持以下数据类型:

    • 列表
    • Python 字典
    • 多维数组
    • 标量值(如,5)

    index 是轴标签列表。不同数据可分为以下几种情况:

    • data 是多维数组时,index 长度必须与 data 长度一致。没有指定 index 参数时,创建数值型索引,即 [0, ..., len(data) - 1]
    • data 为字典,且未设置 index 参数时,如果 Python 版本 >= 3.6 且 Pandas 版本 >= 0.23,Series 按字典的插入顺序排序索引;Python < 3.6 或 Pandas < 0.23,且未设置 index 参数时,Series 按字母顺序排序字典的键(key)列表。如果设置了 index 参数,则按索引标签提取 data 里对应的值。
    • data 是标量值时,必须提供索引。Series 按索引长度重复该标量值。

    dtype表示数据类型,如果没有提供,则会自动判断得出。

    copy表示对 data 进行拷贝,默认为 False。

    1. import numpy as np
    2. import pandas as pd
    3. s = pd.Series([10,20,30])
    4. print(s)
    5. ‘’'
    6. 0 10
    7. 1 20
    8. 2 30
    9. dtype: int64
    10. ‘’'
    11. s = pd.Series({'Name':'John', 'Age':10, 'Score':98})
    12. print(s)
    13. ‘’'
    14. Name John
    15. Age 10
    16. Score 98
    17. dtype: object
    18. ‘''
    19. s = pd.Series(5, index=['First', 'Second', 'Third'])
    20. print(s)
    21. ‘’'
    22. First 5
    23. Second 5
    24. Third 5
    25. dtype: int64
    26. ''
    27. s = pd.Series(np.asarray(5), index=['a', 'b', 'c', 'd', 'e'])
    28. print(s)
    29. ‘’'
    30. a 5
    31. b 5
    32. c 5
    33. d 5
    34. e 5
    35. dtype: int64
    36. ‘’'
    37. print(s.array)
    38. ‘’'
    39. [5, 5, 5, 5, 5]
    40. Length: 5, dtype: int64
    41. ‘’'

    从上面的输出可以看出,Series也是支持dtype的,实际也可以通过属性array访问到Series的数组,Pandas使用的是基于NumPy类型的扩展数组。

    访问Series的数据

    Series的数据可以通过两种方式访问:位置索引访问、索引标签访问。

    1. s = pd.Series([1,2,3,4,5], index=['a', 'b', 'c', 'd', 'e'])
    2. print(s[0]) #1
    3. print(s[-1])#5
    4. print(s['b']) #2

    上面的例子如果使用位置索引时会有警告:FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`

    如果不指定index就可以直接使用位置索引。

    Series也支持负数索引,与NumPy的数组是一样的。

    Series也支持切片:

    1. s = pd.Series([1,2,3,4,5])
    2. print(s[0]) #1
    3. print(s[2:3]) #2 3
    4. print(s[::2]) #1 3 5
    5. ‘’'
    6. 1
    7. 2 3
    8. dtype: int64
    9. 0 1
    10. 2 3
    11. 4 5
    12. dtype: int64
    13. ‘''

    使用索引标签访问多个元素值,需要把标签放在二位数组里:

    1. s = pd.Series([1,2,3,4,5], index=['a', 'b', 'c', 'd', 'e'])
    2. print(s[['b', 'c', 'a']])
    3. ‘’'
    4. b 2
    5. c 3
    6. a 1
    7. dtype: int64
    8. ‘''

    Series常用属性

    名称属性
    axes以列表的形式返回所有行索引标签。
    dtype返回对象的数据类型。
    empty返回一个布尔值,用于判断数据对象是否为空。
    ndim查看序列的维数。根据定义,Series 是一维数据结构,因此它始终返回 1。
    size返回输入数据的元素数量。
    values以 ndarray 的形式返回 Series 对象。
    array返回NumPy的数组对象
    index返回一个RangeIndex对象,用来描述索引的取值范围。
    iloc[...]下标访问元素

    hasnans

    返回是否有空元素(NaN)
    is_unique返回s中的值是不是都是唯一的,如果是返回True
    is_monotonic_increasing如果s中的值是单调增长的,返回True
    is_monotonic_decreasing如果s中的值是单调递减的,返回True
    1. s = pd.Series([1,2,3,4,5], index=['a', 'b', 'c', 'd', 'e'])
    2. print(f'{s.axes=},{s.dtype=},{s.ndim=},{s.empty=}, {s.size=}')
    3. print(f'{s.values=}')
    4. print(f'{s.array=}')
    5. print(f'{s.index=}')
    6. print(f'{s.shape=}’)
    7. print(f'{s.hasnans=}')
    8. ‘’'
    9. s.axes=[Index(['a', 'b', 'c', 'd', 'e'], dtype='object')],s.dtype=dtype('int64'),s.ndim=1,s.empty=False, s.size=5
    10. s.values=array([1, 2, 3, 4, 5])
    11. s.array=
    12. [1, 2, 3, 4, 5]
    13. Length: 5, dtype: int64
    14. s.index=Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
    15. s.shape=(5,)
    16. s.hasnans=False
    17. ''
    1. s = pd.Series([1,2,3,4,5], index=['a', 'b', 'c', 'd', 'e'])
    2. print(s.iloc[0]) #1
    3. print(s.iloc[2:])
    4. print(s.iloc[::-1])
    5. ‘’'
    6. 1
    7. c 3
    8. d 4
    9. e 5
    10. dtype: int64
    11. e 5
    12. d 4
    13. c 3
    14. b 2
    15. a 1
    16. dtype: int64
    17. ‘''
    1. #s中的值全部是唯一的
    2. >>>s = pd.Series([1, 2, 3])
    3. >>>s.is_unique
    4. True
    5. >>>s = pd.Series([1, 2, 3, 1])
    6. >>>s.is_unique
    7. False
    8. #s中的值是否为单调增长
    9. >>>s = pd.Series([1, 2, 2])
    10. >>>s.is_monotonic_increasing
    11. True
    12. >>>s = pd.Series([3, 2, 1])
    13. >>>s.is_monotonic_increasing
    14. False
    15. #s中的值是否为单调减少
    16. >>>s = pd.Series([3, 2, 2, 1])
    17. >>>s.is_monotonic_decreasing
    18. True
    19. >>>s = pd.Series([1, 2, 3])
    20. >>>s.is_monotonic_decreasing
    21. False

    Series支持的运算

    运算说明
    s.add(other[, level, fill_value, axis])s+other
    s.sub(other[, level, fill_value, axis])s-other
    s.mul(other[, level, fill_value, axis])s*other
    s.div(other[, level, fill_value, axis])s/other
    s.truediv(other[, level, fill_value, axis])s/other
    s.floordiv(other[, level, fill_value, axis])s//other
    s.mod(other[, level, fill_value, axis])s%other
    s.pow(other[, level, fill_value, axis])s**other
    s.radd(other[, level, fill_value, axis])s+other
    s.rsub(other[, level, fill_value, axis])s-other
    s.rmul(other[, level, fill_value, axis])s*other
    s.rdiv(other[, level, fill_value, axis])s/other
    s.rtruediv(other[, level, fill_value, axis])s/other
    s.rfloordiv(other[, level, fill_value, axis])s//other
    s.rmod(other[, level, fill_value, axis])s%other
    s.rpow(other[, level, fill_value, axis])s**other
    s.combine(other, func[, fill_value])分别对s、other的每对元素调用func,返回的结果为func返回的结果得到的Series。
    s.combine_first(other])使用other填充s对应的空值
    s.round(decimals=0*args**kwargs)每个元素四舍五入
    s.lt(other[, level, fill_value, axis])s
    s.gt(other[, level, fill_value, axis])s>other
    s.le(other[, level, fill_value, axis])s<=other
    s.ge(other[, level, fill_value, axis])s>=other
    s.ne(other[, level, fill_value, axis])s!=other
    s.eq(other[, level, fill_value, axis])s==other
    s.product(axis=Noneskipna=Truenumeric_only=Falsemin_count=0**kwargs)

    所有元素的乘积

    skipna:是否跳过空值;numeric_only仅数字;min_count最少几个数

    s.dot(other)两个Series求笛卡尔积
    s.abs()对每个元素求绝对值
    简单运算符举例 
    1. import pandas as pd
    2. s1 = pd.Series([1,2,3,4,5])
    3. s2 = pd.Series([10,20,30,40,50])
    4. s3 = s2 - s1
    5. print(f's3=s2 - s1,s3:\n', s3)
    6. s4 = s2.sub(s1) #sub和-实际是等效果的
    7. print(f's4=s2.sub(s1),s4:\n',s4)
    8. print(f's3 == s4 :\n', s3 == s4)
    9. ‘’'
    10. s3=s2 - s1,s3:
    11. 0 9
    12. 1 18
    13. 2 27
    14. 3 36
    15. 4 45
    16. dtype: int64
    17. s4=s2.sub(s1),s4:
    18. 0 9
    19. 1 18
    20. 2 27
    21. 3 36
    22. 4 45
    23. dtype: int64
    24. s3 == s4 :
    25. 0 True
    26. 1 True
    27. 2 True
    28. 3 True
    29. 4 True
    30. dtype: bool
    31. ‘''
    组合调用函数combine
    1. import numpy as np
    2. import pandas as pd
    3. import operator
    4. s1 = pd.Series([11,12,33,24,51])
    5. s2 = pd.Series([10,20,30,40,50])
    6. s5 = s1.combine(s2, max)
    7. print('s5 = s1.combine(s2, max)\n', s5)
    8. s6 = s1.combine(s2, operator.add) #func接受两个参数
    9. print('s6 = s1.combine(s2, operator.add)\n', s6)
    10. #测试fill_value
    11. s3 = pd.Series([10,20,30,None,50])
    12. print('s3:\n', s3)
    13. s4 = s1.combine(s3, max) #有没有fill_value=0,max都可以处理
    14. print('s4 = s1.combine(s3, max):\n', s4)
    15. s4 = s1.combine(s3, max, fill_value=0)
    16. print('s4 = s1.combine(s3, max, fill_value=0):\n', s4)
    17. s7 = s1.combine(s3, operator.add, fill_value=0) #add处理不了
    18. print('s7 = s1.combine(s3,operator.add, fill_value=0)\n', s7)
    19. def foo(*args):
    20. print(f'foo {args=}')
    21. return 0
    22. s7 = s1.combine(s3,foo, fill_value=0) #可以看到传入func的参数None并未被替换为0
    23. print('s7 = s1.combine(s3,foo, fill_value=0)\n', s7)
    24. ‘’'
    25. s5 = s1.combine(s2, max)
    26. 0 11
    27. 1 20
    28. 2 33
    29. 3 40
    30. 4 51
    31. dtype: int64
    32. s6 = s1.combine(s2, operator.add)
    33. 0 21
    34. 1 32
    35. 2 63
    36. 3 64
    37. 4 101
    38. dtype: int64
    39. s3:
    40. 0 10.0
    41. 1 20.0
    42. 2 30.0
    43. 3 NaN
    44. 4 50.0
    45. dtype: float64
    46. s4 = s1.combine(s3, max):
    47. 0 11
    48. 1 20
    49. 2 33
    50. 3 24
    51. 4 51
    52. dtype: int64
    53. s4 = s1.combine(s3, max, fill_value=0):
    54. 0 11
    55. 1 20
    56. 2 33
    57. 3 24
    58. 4 51
    59. dtype: int64
    60. s7 = s1.combine(s3,operator.add, fill_value=0)
    61. 0 21.0
    62. 1 32.0
    63. 2 63.0
    64. 3 NaN
    65. 4 101.0
    66. dtype: float64
    67. foo args=(11, 10.0)
    68. foo args=(12, 20.0)
    69. foo args=(33, 30.0)
    70. foo args=(24, nan)
    71. foo args=(51, 50.0)
    72. s7 = s1.combine(s3,foo, fill_value=0)
    73. 0 0
    74. 1 0
    75. 2 0
    76. 3 0
    77. 4 0
    78. dtype: int64
    79. ‘''
    填充空值combine_first
    1. import pandas as pd
    2. s1 = pd.Series([10,None,30,None,50])
    3. s2 = pd.Series([1,2,None,4,5])
    4. s3 = s1.combine_first(s2)
    5. print(s3)
    6. ’’’
    7. 0 10.0
    8. 1 2.0
    9. 2 30.0
    10. 3 4.0
    11. 4 50.0
    12. dtype: float64
    13. ‘’‘
    连乘product
    1. import pandas as pd
    2. s1 = pd.Series([1,2,3,4,5])
    3. print(s1.product()) #120
    4. s2 = pd.Series([1,2,3,None,5])
    5. print(s2.product()) #15
    6. print(s2.product(skipna=False)) #nan
    7. print(s2.product(skipna=False, min_count=1)) #nan

    Series支持的其他方法

    运算说明
    s.abs()
    s.all([axis, bool_only, skipna])
    s.any(*[, axis, bool_only, skipna])
    s.autocorr([lag])
    s.between(left, right[, inclusive])
    s.clip([lower, upper, axis, inplace])
    s.corr(other[, method, min_periods])
    s.count()
    s.cov(other[, min_periods, ddof])
    s.cummax([axis, skipna])
    s.cummin([axis, skipna])
    s.cumprod([axis, skipna])
    s.cumsum([axis, skipna])
    s.describe([percentiles, include, exclude])
    s.diff([periods])
    s.factorize([sort, use_na_sentinel])
    s.kurt([axis, skipna, numeric_only])
    s.max([axis, skipna, numeric_only])
    s.mean([axis, skipna, numeric_only])
    s.median([axis, skipna, numeric_only])
    s.min([axis, skipna, numeric_only])
    s.mode([dropna])
    s.nlargest([n, keep])
    s.pct_change([periods, fill_method, ...])
    s.prod([axis, skipna, numeric_only, ...])
    s.quantile([q, interpolation])
    s.rank([axis, method, numeric_only, ...])
    s.sem([axis, skipna, ddof, numeric_only])
    s.skew([axis, skipna, numeric_only])
    s.std([axis, skipna, ddof, numeric_only])
    s.sum([axis, skipna, numeric_only, ...])
    s.var([axis, skipna, ddof, numeric_only])
    s.kurtosis([axis, skipna, numeric_only])
    s.unique()
    s.nunique([dropna])
    s.value_counts([normalize, sort, ...])

    Series常用方法

    Series提供的方法非常多,这里列举了一些常见的方法

    元素查询方法
    方法名说明
    s.head(n)返回前 n 行数据,默认返回前 5 行数据
    s.tail(n)返回后 n 行数据,默认返回后 5 行数据
    pd.isnull(s)检测 Series 中的缺失值,如果有值不存在或缺失(NaN),返回True
    pd.notnull(s)检测 Series 中的缺失值,如果有值不存在或缺失(NaN),返回False
    s.get(key[,default])通过索引获取值
    s.at[index]通过索引访问值
    s.iat[iloc]通过整数索引访问值
    s.loc[index]通过索引访问值
    s.iloc[iloc]通过整数索引访问值
    s.__iter__()返回元素的迭代器
    s.items()返回(index,value)的zip对象,可以通过list转化为列表
    s.keys()返回index对象
    s.isin(values)逐个检查s中的元素,看是否在values中,得到一个新的bool的Series
    s.where(cond[, other, inplace, axis, level])按条件查询,如果条件为假,可以使用other取代
    s.mask(cond[, other, inplace, axis, level])按条件查询,如果条件为真,可以使用other取代
    s.filter([items, like, regex, axis])按索引过滤

    1. import pandas as pd
    2. s1 = pd.Series([1, 2, 3, 4, 5], index=['a', 'b', 'c', 'd', 'e'])
    3. print(s1.get('b')) #2
    4. print(s1.at['c']) #3
    5. print(s1.iat[4]) #5
    6. print(s1.iat[-2]) #4
    7. print(s1.loc['a']) #1
    8. print(s1.iloc[3]) #4
    9. print(list(s1.items())) #[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
    10. print(s1.keys()) #Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
    11. bv = s1.pop('b')
    12. print(f's1.pop(b)后的s1:{bv=}\n', s1)
    13. ‘’'
    14. 2
    15. 3
    16. 5
    17. 4
    18. 1
    19. 4
    20. [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
    21. Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
    22. s1.pop(b)后的s1:bv=2
    23. a 1
    24. c 3
    25. d 4
    26. e 5
    27. dtype: int64
    28. ‘''

    1. import pandas as pd
    2. s1 = pd.Series([10,20,30,40,50], index=['A', 'B', 'C', 'D', 'E'])
    3. print("s1>30:\n", s1.where(s1>30))
    4. ’’’
    5. s1>30:
    6. A NaN
    7. B NaN
    8. C NaN
    9. D 40.0
    10. E 50.0
    11. dtype: float64
    12. ‘’‘
    1. import pandas as pd
    2. s1 = pd.Series([10,20,30,40,50], index=['A', 'B', 'C', 'D', 'E'])
    3. print("s1>30:\n", s1.where(s1>30))
    4. print("s1>30,other=[1]:\n", s1.where(s1>30, other=[1]))
    5. print("s1>30,other=[1]:\n", s1.mask(s1>30, other=[1]))
    6. print("s1.filter(items=['A', 'B']):\n", s1.filter(items=['A', 'B']))
    7. print("s1.filter(regex=['ABC']:\n", s1.filter(regex="['ABC']”))
    8. ‘’'
    9. s1>30:
    10. A NaN
    11. B NaN
    12. C NaN
    13. D 40.0
    14. E 50.0
    15. dtype: float64
    16. s1>30,other=[1]:
    17. A 1
    18. B 1
    19. C 1
    20. D 40
    21. E 50
    22. dtype: int64
    23. s1>30,other=[1]:
    24. A 10
    25. B 20
    26. C 30
    27. D 1
    28. E 1
    29. dtype: int64
    30. s1.filter(items=['A', 'B']):
    31. A 10
    32. B 20
    33. dtype: int64
    34. s1.filter(regex=['ABC']:
    35. A 10
    36. B 20
    37. C 30
    38. dtype: int64
    39. ‘''

    复制和类型变换
    方法名说明
    s.copy(deep=True)深拷贝,返回一个复制的Series,如果deep=False将得到一个浅拷贝
    s.to_list()将Series转换为list结构返回
    s.apply(func[, convert_dtype, args, by_row])对Series的每个值调用func函数
    s.astype(dtypecopy=Noneerrors='raise')将s元素的类型进行变换为dtype指定的类型
    s.to_numpy(dtype=Nonecopy=Falsena_value=_NoDefault.no_default**kwargs)将s转化为NumPy数组
    s.__array__(dtype=None)返回s底层的NumPy数组,如果改变了NumPy数组,s的元素值也会变化
    s.to_pickle(path[, compression, ...])将s序列化写入文件
    s.to_csv([path_or_buf, sep, na_rep, ...])将s写入csv文件
    s.to_dict([into])将s转为dict
    s.to_excel(excel_writer[, sheet_name, ...])将s写入excel文件
    s.to_frame([name])将s转换为DataFrame
    s.to_xarray()将s转换为xarray对象
    s.to_hdf(path_or_buf, key[, mode, ...])将s写入为HDFS文件
    s.to_sql(name, con, *[, schema, ...])将s转为为sql语句
    s.to_json([path_or_buf, orient, ...])将s转化为json对象
    s.to_string([buf, na_rep, ...])将s转化为string对象
    s.to_clipboard([excel, sep])拷贝s对象到系统剪切板
    s.to_latex([buf, columns, header, ...])转换s为LaTeX
    s.to_markdown([buf, mode, index, ...])转换s为MarkDown

    1. import pandas as pd
    2. s1 = pd.Series([1, 2], dtype='int32')
    3. print('s1:\n', s1)
    4. s2 = s1.astype('float32')
    5. print('s2:\n',s2)
    6. s3 = s1.astype('int16', copy=False)
    7. print('s3:\n',s3)
    8. s3[0] = 10
    9. print('修改s3后的s1:\n',s1)
    10. ‘’'
    11. s1:
    12. 0 1
    13. 1 2
    14. dtype: int32
    15. s2:
    16. 0 1.0
    17. 1 2.0
    18. dtype: float32
    19. s3:
    20. 0 1
    21. 1 2
    22. dtype: int16
    23. 修改s3后的s1:
    24. 0 1
    25. 1 2
    26. dtype: int32
    27. ‘''
    1. import pandas as pd
    2. s1 = pd.Series([1, 2, 3, 4, 5])
    3. a = s1.to_numpy()
    4. print(f'{type(a)=}',a)
    5. a2 = s1.__array__()
    6. a2[1] = 10
    7. print(s1)
    8. ‘’'
    9. type(a)=numpy.ndarray'> [1 2 3 4 5]
    10. 0 1
    11. 1 10
    12. 2 3
    13. 3 4
    14. 4 5
    15. dtype: int64
    16. ‘''
    1. s1 = pd.Series([None, None, 3, 4, None], index=['A', 'B', 'C', 'D', 'E'])
    2. print(s1.to_dict())
    3. #{'A': nan, 'B': nan, 'C': 3.0, 'D': 4.0, 'E': nan}

    排序
    方法名说明
    s.argsort([axis, kind, order])返回排序的整数下标,一个新的Series
    s.argmin([axis, skipna])返回最小值的下标位置,多个只返回第一个
    s.argmax([axis, skipna])返回最大值的下标位置,多个只返回第一个
    s.sort_values(*[, axis, ascending, ...])按值进行排序,将得到一个新的Series
    s.sort_index(*[, axis, level, ...])按索引排序,将得到一个新的Series
    s.reorder_levels(order)s有多个索引的情况,重组索引的排列顺序,order是重新组织的索引序号的排列
    s.swaplevel([i, j, copy])s有多个索引的情况,交换索引,i,j是索引的序号
    s.unstack([level, fill_value, sort])s有多个索引的情况,将s转换为DataFrame,level指定的是索引序号
    s.explode([ignore_index])把有复杂元素的Series拉平成一维的Series
    s.searchsorted(value[, side, sorter])在已排序的Series中插入value,返回value应该插入的下标位置,如果Series不是已经排序好的,可能会找到第一个认为合适的位置
    s.ravel([order])返回底层数组
    s.repeat(repeats[, axis])循环复制s的元素repeats次,得到一个新的Series
    s.view([dtype])创建一个s的视图
    1. import pandas as pd
    2. s1 = pd.Series([5,4,3,2,1], index=['A','B','C','D','E'])
    3. s2 = s1.argsort()
    4. print('s2 = s1.argsort():\n', s2)
    5. s1 = pd.Series([5,5,3,1,1], index=['A','B','C','D','E'])
    6. print(f'{s1.argmin()=}')
    7. print(f'{s1.argmax()=}')
    8. s1 = pd.Series([5,5,3,1,1], index=['A','B','A','B','A'])
    9. print('s1.sort_values():\n', s1.sort_values())
    10. print('s1.sort_index():\n', s1.sort_index())
    11. s2 = pd.Series([5,5,3,1,1], index=[['A','B','A','B','A'],['S5','S2','S4','S3','S1'],['C1','C2','C3','C4','C5']])
    12. print('s2.reorder_levels([1,0,2]:\n', s2.reorder_levels([1,0,2]))
    13. print('s2.swaplevel(0):\n', s2.swaplevel(0))
    14. print('s2.swaplevel(1,2):\n', s2.swaplevel(1,2))
    15. print('s2.unstack(level=1,fill_value=0):\n', s2.unstack(level=1,fill_value=0))
    16. s3 = pd.Series([[1,2,3], 'foo', [], [5,6]])
    17. print(f's3:\n{s3}')
    18. print('s3.explode():\n', s3.explode())
    19. s1 = pd.Series([1,2,3,4,5],index=['A','B','A','B','A'])
    20. print(f'{s1.searchsorted(4)=}')
    21. s1 = pd.Series([3,4,5,2,1],index=['A','B','A','B','A'])
    22. print(f'{s1.searchsorted(4)=}')
    23. print('s1.repeat(2):\n', s1.repeat(2))
    24. s2 = s1.view('int64')
    25. s2['A'] = 1234567890
    26. print(f'{s1=}')
    27. ‘’’
    28. s2 = s1.argsort():
    29. A 4
    30. B 3
    31. C 2
    32. D 1
    33. E 0
    34. dtype: int64
    35. s1.argmin()=3
    36. s1.argmax()=0
    37. s1.sort_values():
    38. B 1
    39. A 1
    40. A 3
    41. A 5
    42. B 5
    43. dtype: int64
    44. s1.sort_index():
    45. A 5
    46. A 3
    47. A 1
    48. B 5
    49. B 1
    50. dtype: int64
    51. s2.reorder_levels([1,0,2]:
    52. S5 A C1 5
    53. S2 B C2 5
    54. S4 A C3 3
    55. S3 B C4 1
    56. S1 A C5 1
    57. dtype: int64
    58. s2.swaplevel(0):
    59. C1 S5 A 5
    60. C2 S2 B 5
    61. C3 S4 A 3
    62. C4 S3 B 1
    63. C5 S1 A 1
    64. dtype: int64
    65. s2.swaplevel(1,2):
    66. A C1 S5 5
    67. B C2 S2 5
    68. A C3 S4 3
    69. B C4 S3 1
    70. A C5 S1 1
    71. dtype: int64
    72. s2.unstack(level=1,fill_value=0):
    73. S1 S2 S3 S4 S5
    74. A C1 0 0 0 0 5
    75. C3 0 0 0 3 0
    76. C5 1 0 0 0 0
    77. B C2 0 5 0 0 0
    78. C4 0 0 1 0 0
    79. s3:
    80. 0 [1, 2, 3]
    81. 1 foo
    82. 2 []
    83. 3 [5, 6]
    84. dtype: object
    85. s3.explode():
    86. 0 1
    87. 0 2
    88. 0 3
    89. 1 foo
    90. 2 NaN
    91. 3 5
    92. 3 6
    93. dtype: object
    94. s1.searchsorted(4)=3
    95. s1.searchsorted(4)=1
    96. s1.repeat(2):
    97. A 3
    98. A 3
    99. B 4
    100. B 4
    101. A 5
    102. A 5
    103. B 2
    104. B 2
    105. A 1
    106. A 1
    107. dtype: int64
    108. s1=A 1234567890
    109. B 4
    110. A 1234567890
    111. B 2
    112. A 1234567890
    113. dtype: int64
    114. ''

    操作单个元素
    方法名说明
    s.drop([labels, axis, index, columns, ...])返回删除指定索引的元素的一个新Series,s没有影响
    s.drop_duplicates(*[, keep, inplace, ...])返回删除重复元素的一个新Series,s没有影响
    s.duplicated([keep])逐个元素判断是否是重复元素,得到一个新的bool值的Series
    s.pop(index)获取index指定的元素,并将该元素从s中删除
    批量操作元素
    方法名说明
    s.all([axis, bool_only, skipna])检查是否所有元素都为True
    s.any(*[, axis, bool_only, skipna])检查是否任意一元素为True
    s.between(left, right[, inclusive])检查元素是否在left和right之间(含边界值),返回bool的Series序列,NaN被认为是False
    s.count()统计Series中非空值的个数
    s.cov(other[, min_periods, ddof])计算s和other的协方差,s和other不要求有相同的长度
    s.cummax([axis, skipna])计算s的累计最大值,就是按顺序比较,如果当前值比当前为止的最大值大,就将当前值作为最大值,始终用最大值填充当前的位置
    s.cummin([axis, skipna])计算s的累计最小值
    s.cumprod([axis, skipna])

    计算s的累计乘积,将得到一个新的Series,命名为ns:

    ns的第0个元素等于s的第0个元素

    ns的第1个元素等于ns的第0个元素与s第1个元素的乘积

    ns的第2个元素等于ns的第1个元素与s第2个元素的乘积

    ...(以此类推)

    s.cumsum([axis, skipna])计算s的累计和
    s.describe([percentiles, include, exclude])得到s的统计信息,对于数字数据,包括count, mean, std, min, max等函数的饿值
    s.diff([periods])

    计算s的两个元素之间的差值:

    第0个差值为NaN

    第1个差值为:第0个与第1个

    第2个差值为:第1个与第2个

    ...

    periods指定起始的位置,如果为-1,就是从最后一个往前算

    s.max([axis, skipna, numeric_only])返回s的最大元素
    s.min([axis, skipna, numeric_only])返回s的最小元素
    s.mean([axis, skipna, numeric_only])返回s的算术平均数
    s.median([axis, skipna, numeric_only])返回s的元素的中位数(不是平均值,大小在中间的那个)
    s.mode([dropna])返回重复次数最多的数,如果有最多的重复数是多个,则返回多个
    s.nlargest([n, keep])返回最大的n个元素
    s.nsmallest([n, keep])返回最小的n个元素
    s.pct_change([periods, fill_method, ...])计算变化的比例:(当前元素-前一个元素)/前一个元素
    s.prod([axis, skipna, numeric_only, ...])返回所有元素的乘积
    s.std([axis, skipna, ddof, numeric_only])求s所有元素的标准差
    s.sum([axis, skipna, numeric_only, ...])求s所有元素的和
    s.var([axis, skipna, ddof, numeric_only])求s所有元素的无偏方差
    s.unique()返回s元素的唯一元素(去重)
    s.nunique([dropna])返回s中唯一元素的个数
    s.equals(other)检查s和other包含的元素是否一致,要求顺序和索引也是一致的
    s.truncate([before, after, axis, copy])截断before前和after后的元素,生成一个新的Series
    s.replace([to_replace, value, inplace, ...])值替换,to_replace指定要替换的值,value替换后的值
    s.compare(other[, align_axis, ...])比较s、other的元素,将有差异的元素生成DataFrame
    s.update(other)用other去更新s
    1. import pandas as pd
    2. s1 = pd.Series([10,20,30,40,50])
    3. s2 = s1.between(20,40) #检查 20<=元素<=40
    4. print(s2)
    5. s1 = pd.Series([10,20,30,None,50])
    6. print(f'{s1.count()=}')
    7. s2 = pd.Series([10,20,60,50,70,66])
    8. print('s2.cummax():\n', s2.cummax())
    9. s1 = pd.Series([1,2,3,4,5])
    10. print('s1.cumprod():\n', s1.cumprod())
    11. print('s1.cumsum():\n', s1.cumsum())
    12. print('s1.describe():\n', s1.describe())
    13. print('s1.diff():\n', s1.diff())
    14. print('s1.diff(periods=0):\n', s1.diff(periods=0))
    15. print('s1.diff(periods=-1):\n', s1.diff(periods=-1))
    16. print(f'{s1.max()=}')
    17. print(f'{s1.min()=}')
    18. print(f'{s1.mean()=}')
    19. s2 = pd.Series([1,2,3,40,50])
    20. print(f'{s2.median()=}')
    21. print(f'{s2.mode()=}')
    22. s3 = pd.Series([2,4,2,4,3,2,5])
    23. print(f'{s3.mode()=}')
    24. print(f'{s1.nlargest(2)=}')
    25. print(f'{s1.nsmallest(2)=}')
    26. print(f'{s1.pct_change()=}')
    27. print(f'{s1.prod()=}')
    28. print(f'{s1.std()=}')
    29. print(f'{s1.var()=}')
    30. print(f'{s3.unique()=}')
    31. print(f'{s1.nunique()=}')
    32. ’’’
    33. 0 False
    34. 1 True
    35. 2 True
    36. 3 True
    37. 4 False
    38. dtype: bool
    39. s1.count()=4
    40. s2.cummax():
    41. 0 10
    42. 1 20
    43. 2 60
    44. 3 60
    45. 4 70
    46. 5 70
    47. dtype: int64
    48. s1.cumprod():
    49. 0 1
    50. 1 2
    51. 2 6
    52. 3 24
    53. 4 120
    54. dtype: int64
    55. s1.cumsum():
    56. 0 1
    57. 1 3
    58. 2 6
    59. 3 10
    60. 4 15
    61. dtype: int64
    62. s1.describe():
    63. count 5.000000
    64. mean 3.000000
    65. std 1.581139
    66. min 1.000000
    67. 25% 2.000000
    68. 50% 3.000000
    69. 75% 4.000000
    70. max 5.000000
    71. dtype: float64
    72. s1.diff():
    73. 0 NaN
    74. 1 1.0
    75. 2 1.0
    76. 3 1.0
    77. 4 1.0
    78. dtype: float64
    79. s1.diff(periods=0):
    80. 0 0.0
    81. 1 0.0
    82. 2 0.0
    83. 3 0.0
    84. 4 0.0
    85. dtype: float64
    86. s1.diff(periods=-1):
    87. 0 -1.0
    88. 1 -1.0
    89. 2 -1.0
    90. 3 -1.0
    91. 4 NaN
    92. dtype: float64
    93. s1.max()=5
    94. s1.min()=1
    95. s1.mean()=3.0
    96. s2.median()=3.0
    97. s2.mode()=0 1
    98. 1 2
    99. 2 3
    100. 3 40
    101. 4 50
    102. dtype: int64
    103. s3.mode()=0 2
    104. dtype: int64
    105. s1.nlargest(2)=4 5
    106. 3 4
    107. dtype: int64
    108. s1.nsmallest(2)=0 1
    109. 1 2
    110. dtype: int64
    111. s1.pct_change()=0 NaN
    112. 1 1.000000
    113. 2 0.500000
    114. 3 0.333333
    115. 4 0.250000
    116. dtype: float64
    117. s1.prod()=120
    118. s1.std()=1.5811388300841898
    119. s1.var()=2.5
    120. s3.unique()=array([2, 4, 3, 5])
    121. s1.nunique()=5
    122. ‘’‘
    1. s7 = pd.Series([20,20,30,30,20])
    2. s8 = s7.replace(20, 100)
    3. print('s8 = s7.replace(1, 100):\n', s8)
    4. ’’’
    5. s8 = s7.replace(1, 100):
    6. 0 100
    7. 1 100
    8. 2 30
    9. 3 30
    10. 4 100
    11. dtype: int64
    12. ‘’‘

     处理空值
    方法名说明

    s.backfill(*[, axis, inplace, limit, ...])

    使用后面非空的值填充空值,得到一个新的Series
    s.bfill(*[, axis, inplace, limit, downcast])使用后面非空的值填充空值,得到一个新的Series
    s.dropna(*[, axis, inplace, how, ...])删除空值,得到一个新的Series

    s.ffill(*[, axis, inplace, limit, downcast])

    s.pad(*[, axis, inplace, limit, downcast])

    使用紧接着的前面值填充空值,得到一个新的Series
    s.fillna([value, method, axis, ...])使用值value或方法method去填充空值。如果value是一个标量值,所有的控制都填充为value;如果value是一个字典dict,key指定的是s的index,将对应的用字典的值去替换key对应的index位置的空值进行替换;如果是method方法,指定的是前面的bfill、backfill、ffill等方法,得到一个新的Series
    s.interpolate([method, axis, limit, ...])使用插值法填充空值,得到一个新的Series

    s.isna()

    s.isnull()

    检测控制,得到一个新的bool类型的Series,对应的元素如果是空值为True,否则为False

    s.notna()

    s.notnull()

    与isna()一样,只是如果是空值则为False
    s.first_valid_index()返回第一个非空值的索引
    s.last_valid_index()返回最后一个非空值的索引

    1. s1 = pd.Series([None, None, 3, 4, None], index=['A', 'B', 'C', 'D', 'E'])
    2. print(s1.first_valid_index()) #C
    高阶函数 
    方法名说明
    s.apply(func[, convert_dtype, args, by_row])对Series的每个值调用func函数,func接收一个参数,当args指定n个参数时,就接收n+1个参数,第一个参数始终是每个元素的值
    s.agg([func, axis])对Series使用聚合函数,func必须聚合函数名的字符串
    s.aggregate([func, axis])对Series使用聚合函数,func必须聚合函数名的字符串
    s.transform(func[, axis])对每一个元素调用func,func只接收一个参数(每个元素轮一遍),结果组成一个新的Series
    s.map(arg[, na_action])如果arg是一个字典dic,就查找dic的key对应的s中的值,如果找不到就填充为NaN,找到就填充为s中的值,生成一个新的Series,如果arg是一个函数,与transform一样
    s.groupby([by, axis, level, as_index, ...])按给定的标识去分组,by作为分组的标识
    s.rolling(window[, min_periods, ...])滑动窗口计算,windows是指计算的元素有几个
    s.expanding([min_periods, axis, method])扩展窗口计算
    s.ewm([com, span, halflife, alpha, ...])指数加权计算

    1. s1 = pd.Series([2,30,4,31,32], index=['A','B','C','D','E'])
    2. sg = s1.groupby(['1','2','1','2','2']) #把5个数按'1','2'进行分组
    3. print(sg.groups) #打印两个分组的索引
    4. #{'1': ['A', 'C'], '2': ['B', 'D', 'E']}
    5. print(sg.mean()) #分组求平均
    6. # 1 3.0
    7. # 2 31.0
    8. # dtype: float64
    1. s1 = pd.Series([1,2,3,4,5,6])
    2. print(s1.rolling(2).sum())
    3. ‘’’
    4. 0 NaN
    5. 1 3.0
    6. 2 5.0
    7. 3 7.0
    8. 4 9.0
    9. 5 11.0
    10. dtype: float64
    11. ’’’
    12. print(s1.expanding(2).sum())
    13. ‘’’
    14. 0 NaN
    15. 1 3.0
    16. 2 6.0
    17. 3 10.0
    18. 4 15.0
    19. 5 21.0
    20. dtype: float64
    21. ’‘’

    DataFrame

    DataFrame 一个表格型的数据结构,类似于 Excel 、SQL 表,既有行标签(index),又有列标签(columns),它也被称异构数据表,所谓异构,指的是表格中每列的数据类型可以不同,比如可以是字符串、整型或者浮点型等。

    DataFrame 的每一行数据都可以看成一个 Series 结构,只不过,DataFrame 为这些行中每个数据值增加了一个列标签。因此 DataFrame 其实是从 Series 的基础上演变而来。在数据分析任务中 DataFrame 的应用非常广泛,因为它描述数据的更为清晰、直观。

    DataFrame 数据结构的特点做简单地总结,如下所示:

    • DataFrame 每一列的标签值允许使用不同的数据类型;
    • DataFrame 是表格型的数据结构,具有行和列;
    • DataFrame 中的每个数据值都可以被修改。
    • DataFrame 结构的行数、列数允许增加或者删除;
    • DataFrame 有两个方向的标签轴,分别是行标签和列标签;
    • DataFrame 可以对行和列执行算术运算。

    DataFrame对象定义

    pd.DataFrame( data, index, columns, dtype, copy)

    参数说明:

    • data:输入的数据,可以是 ndarray,series,list,dict,标量以及一个 DataFrame。
    • index:行标签,如果没有传递 index 值,则默认行标签是 np.arange(n),n 代表 data 的元素个数。
    • columns:列标签,如果没有传递 columns 值,则默认列标签是 np.arange(n)。
    • dtype:dtype表示每一列的数据类型。
    • copy:默认为 False,表示复制数据 data。

    创建一个空的DataFrame:

    1. import pandas as pd
    2. df = pd.DataFrame()
    3. print(df)
    4. ‘’’
    5. Empty DataFrame
    6. Columns: []
    7. Index: []
    8. ’‘’
    通过list创建DataFrame

    可以通过list创建一个简单的只有一列的DataFrame,如:

    1. import pandas as pd
    2. df = pd.DataFrame([1,2,3,4,5,6])
    3. print(df)
    4. ‘’’
    5. 0
    6. 0 1
    7. 1 2
    8. 2 3
    9. 3 4
    10. 4 5
    11. 5 6
    12. ’‘’
    13. df = pd.DataFrame([1,2,3,4,5,6], columns=['No']) #指定列名
    14. print(df)
    15. ‘’’
    16. No
    17. 0 1
    18. 1 2
    19. 2 3
    20. 3 4
    21. 4 5
    22. 5 6
    23. ’‘’

    也可以通过嵌套列,创建多列的DataFrame:

    1. df = pd.DataFrame([['Alex', 10], ['John', 13], ['Rose', 8]], columns=['Name', 'Age'])
    2. print(df)
    3. ‘’'
    4. Name Age
    5. 0 Alex 10
    6. 1 John 13
    7. 2 Rose 8
    8. ‘''
    通过dict创建DataFrame

    通过dict创建DataFrame,每个key都是一列,value是具体的列值(一般为list),要求value的list是等长的。

    1. import pandas as pd
    2. data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'],'Age':[28,34,29,42]}
    3. df = pd.DataFrame(data)
    4. print(df)
    5. ’’’
    6. Age Name
    7. 0 28 Tom
    8. 1 34 Jack
    9. 2 29 Steve
    10. 3 42 Ricky
    11. ‘’‘

    也可以通过列表中嵌套字典的方式,列表的每个元素都是一行,而嵌套的字典的key是列名,要求字典的key是一样的。

    1. df = pd.DataFrame([{'Name':'Alex', 'Age':10}, {'Name':'John', 'Age':13}, {'Name': 'Rose', 'Age': 8}])
    2. print(df)
    3. ‘’'
    4. Name Age
    5. 0 Alex 10
    6. 1 John 13
    7. 2 Rose 8
    8. ‘''
    通过Series创建DataFrame

    可以传递一个字典形式的 Series,从而创建一个 DataFrame 对象,其输出结果的行索引是所有 index 的并集

    1. df = pd.DataFrame({'Name':pd.Series(['Tom', 'Jack', 'Steve', 'Ricky','Bob'], index=['A', 'B', 'C', 'D', 'E']),
    2. 'Age':pd.Series([28,34,29,42], index=['A', 'B', 'C', 'D'])})
    3. print(df)
    4. ‘’'
    5. Name Age
    6. A Tom 28.0
    7. B Jack 34.0
    8. C Steve 29.0
    9. D Ricky 42.0
    10. E Bob NaN
    11. ‘''

    注意:两个Series的索引一定要一样或大致一样,生成的DataFrame的行是两个Series的索引的并集,只有索引一样的对应的元素才会被整合在一行。

    1. df = pd.DataFrame({'Name':pd.Series(['Tom', 'Jack', 'Steve', 'Ricky','Bob'], index=['A', 'B', 'C', 'D', 'E']),
    2. 'Age':pd.Series([28,34,29,42])})
    3. Name Age
    4. A Tom NaN
    5. B Jack NaN
    6. C Steve NaN
    7. D Ricky NaN
    8. E Bob NaN
    9. 0 NaN 28.0
    10. 1 NaN 34.0
    11. 2 NaN 29.0
    12. 3 NaN 42.0
    其他构建器
    函数和方法名说明
    DataFrame.from_dict(dict)接收字典组成的字典或数组序列字典,并生成 DataFrame
    DataFrame.from_records支持元组列表或结构数据类型(dtype)的多维数组

    列索引操作

    选取数据列

    可以直接通过列索引下标获取列:

    1. df = pd.DataFrame({'Name':pd.Series(['Tom', 'Jack', 'Steve', 'Ricky','Bob'], index=['A', 'B', 'C', 'D', 'E']),
    2. 'Age':pd.Series([28,34,29,42], index=['A', 'B', 'C', 'D'])})
    3. print('df:\n', df)
    4. print('df["Name"]:\n', df["Name"])
    5. print('df["Age"]:\n', df["Age”])
    6. ‘’'
    7. df:
    8. Name Age
    9. A Tom 28.0
    10. B Jack 34.0
    11. C Steve 29.0
    12. D Ricky 42.0
    13. E Bob NaN
    14. df["Name"]:
    15. A Tom
    16. B Jack
    17. C Steve
    18. D Ricky
    19. E Bob
    20. Name: Name, dtype: object
    21. df["Age"]:
    22. A 28.0
    23. B 34.0
    24. C 29.0
    25. D 42.0
    26. E NaN
    27. Name: Age, dtype: float64
    28. ‘''
    增加数据列

    也可以直接通过列索引增加数据列,主要注意的是新增的列,索引一定要匹配,否则会增加一个全部为NaN值的列:

    1. df = pd.DataFrame({'Name':pd.Series(['Tom', 'Jack', 'Steve', 'Ricky','Bob'], index=['A', 'B', 'C', 'D', 'E']),
    2. 'Age':pd.Series([28,34,29,42], index=['A', 'B', 'C', 'D'])})
    3. df['Score'] = pd.Series([90, 58, 99, 100, 48], index=['A', 'B', 'C', 'D', 'E'])
    4. print(df)
    5. ’’’
    6. Name Age Score
    7. A Tom 28.0 90
    8. B Jack 34.0 58
    9. C Steve 29.0 99
    10. D Ricky 42.0 100
    11. E Bob NaN 48
    12. ''
    13. df['English'] = pd.Series([100, 100, 80, 100, 70])
    14. print(df)
    15. ‘’'
    16. Name Age Score English
    17. A Tom 28.0 90 NaN
    18. B Jack 34.0 58 NaN
    19. C Steve 29.0 99 NaN
    20. D Ricky 42.0 100 NaN
    21. E Bob NaN 48 NaN
    22. ‘''

    也可以直接引用DataFrame的列进行运算,增加计算列:

    1. df = pd.DataFrame({'Name':pd.Series(['Tom', 'Jack', 'Steve', 'Ricky','Bob'], index=['A', 'B', 'C', 'D', 'E']),
    2. 'Age':pd.Series([28,34,29,42], index=['A', 'B', 'C', 'D'])})
    3. df['Math'] = pd.Series([90, 58, 99, 100, 48], index=['A', 'B', 'C', 'D', 'E'])
    4. df['English'] = pd.Series([100, 100, 80, 100, 70], index=['A', 'B', 'C', 'D', 'E'])
    5. df['Total'] = df['Math'] + df['English']
    6. print(df)
    7. ‘’'
    8. Name Age Math English Total
    9. A Tom 28.0 90 100 190
    10. B Jack 34.0 58 100 158
    11. C Steve 29.0 99 80 179
    12. D Ricky 42.0 100 100 200
    13. E Bob NaN 48 70 118
    14. ‘''
    插入数据列

    通过insert方法可以插入一列:

    DataFrame.insert(loc, column, value, allow_duplicates=_NoDefault.no_default)

    参数说明:

    • loc:插入索引的位置,必须是0 <= loc <= len(columns).
    • column:要插入的列名
    • value:插入的列的值,一般是Series或者可以转换为Series的类型
    • allow_duplicates:是否允许重复
    1. df = pd.DataFrame({'Name':pd.Series(['Tom', 'Jack', 'Steve', 'Ricky','Bob'], index=['A', 'B', 'C', 'D', 'E']),
    2. 'Age':pd.Series([28,34,29,42], index=['A', 'B', 'C', 'D'])})
    3. df['Math'] = pd.Series([90, 58, 99, 100, 48], index=['A', 'B', 'C', 'D', 'E'])
    4. df['English'] = pd.Series([100, 100, 80, 100, 70], index=['A', 'B', 'C', 'D', 'E'])
    5. df.insert(2, 'Chinese', [100,99,98,96,90])
    6. print(df)
    7. ’’’
    8. Name Age Chinese Math English
    9. A Tom 28.0 100 90 100
    10. B Jack 34.0 99 58 100
    11. C Steve 29.0 98 99 80
    12. D Ricky 42.0 96 100 100
    13. E Bob NaN 90 48 70
    14. ‘’‘
    删除数据列

    通过 del 和 pop() 都能够删除 DataFrame 中的数据列。

    1. df = pd.DataFrame({'Name':pd.Series(['Tom', 'Jack', 'Steve', 'Ricky','Bob'], index=['A', 'B', 'C', 'D', 'E']),
    2. 'Age':pd.Series([28,34,29,42], index=['A', 'B', 'C', 'D'])})
    3. df['Math'] = pd.Series([90, 58, 99, 100, 48], index=['A', 'B', 'C', 'D', 'E'])
    4. df['English'] = pd.Series([100, 100, 80, 100, 70], index=['A', 'B', 'C', 'D', 'E'])
    5. del df['Age']
    6. print(df)
    7. ‘’'
    8. Name Math English
    9. A Tom 90 100
    10. B Jack 58 100
    11. C Steve 99 80
    12. D Ricky 100 100
    13. E Bob 48 70
    14. ‘''

    pop()方法的定义如下:

    DataFrame.pop(item)

    参数说明:

    • item:列名
    1. df = pd.DataFrame({'Name':pd.Series(['Tom', 'Jack', 'Steve', 'Ricky','Bob'], index=['A', 'B', 'C', 'D', 'E']),
    2. 'Age':pd.Series([28,34,29,42], index=['A', 'B', 'C', 'D'])})
    3. df['Math'] = pd.Series([90, 58, 99, 100, 48], index=['A', 'B', 'C', 'D', 'E'])
    4. df['English'] = pd.Series([100, 100, 80, 100, 70], index=['A', 'B', 'C', 'D', 'E'])
    5. df.pop('Age')
    6. print(df)
    7. ‘’'
    8. Name Math English
    9. A Tom 90 100
    10. B Jack 58 100
    11. C Steve 99 80
    12. D Ricky 100 100
    13. E Bob 48 70
    14. ‘''

    行索引操作

    选取数据行

    行索引操作,需要使用loc属性,使用中括号引用行,中括号内是行索引标识:

    1. df = pd.DataFrame({'Name':pd.Series(['Tom', 'Jack', 'Steve', 'Ricky','Bob'], index=['A', 'B', 'C', 'D', 'E']),
    2. 'Age':pd.Series([28,34,29,42], index=['A', 'B', 'C', 'D'])})
    3. df['Math'] = pd.Series([90, 58, 99, 100, 48], index=['A', 'B', 'C', 'D', 'E'])
    4. df['English'] = pd.Series([100, 100, 80, 100, 70], index=['A', 'B', 'C', 'D', 'E'])
    5. print(df.loc['B’])
    6. ’’’
    7. Name Jack
    8. Age 34.0
    9. Math 58
    10. English 100
    11. Name: B, dtype: object
    12. ‘’‘

    loc属性的中括号中也可以指定两个参数,第一个是行的索引标识,第二个是列名:

    1. df = pd.DataFrame({'Name':pd.Series(['Tom', 'Jack', 'Steve', 'Ricky','Bob'], index=['A', 'B', 'C', 'D', 'E']),
    2. 'Age':pd.Series([28,34,29,42], index=['A', 'B', 'C', 'D'])})
    3. df['Math'] = pd.Series([90, 58, 99, 100, 48], index=['A', 'B', 'C', 'D', 'E'])
    4. df['English'] = pd.Series([100, 100, 80, 100, 70], index=['A', 'B', 'C', 'D', 'E'])
    5. print(df.loc['B', 'Age’]) #34.0
    6. print(df.loc['B':'D’])
    7. ‘’'
    8. Name Age Math English
    9. B Jack 34.0 58 100
    10. C Steve 29.0 99 80
    11. D Ricky 42.0 100 100
    12. ‘''

    也可以使用切片,如上例。

    也支持整数下标索引,需要使用iloc属性:

    1. df = pd.DataFrame({'Name':pd.Series(['Tom', 'Jack', 'Steve', 'Ricky','Bob'], index=['A', 'B', 'C', 'D', 'E']),
    2. 'Age':pd.Series([28,34,29,42], index=['A', 'B', 'C', 'D'])})
    3. df['Math'] = pd.Series([90, 58, 99, 100, 48], index=['A', 'B', 'C', 'D', 'E'])
    4. df['English'] = pd.Series([100, 100, 80, 100, 70], index=['A', 'B', 'C', 'D', 'E'])
    5. print(df.iloc[1])
    6. ‘’’
    7. Name Jack
    8. Age 34.0
    9. Math 58
    10. English 100
    11. Name: B, dtype: object
    12. ’’’
    13. print(df.iloc[1:3])
    14. ‘’’
    15. Name Age Math English
    16. B Jack 34.0 58 100
    17. C Steve 29.0 99 80
    18. ’’’
    19. print(df.iloc[1, 2]) #58
    增加数据行

    可以像增加列一样,直接对loc进行行增加:

    1. df = pd.DataFrame({'Name':pd.Series(['Tom', 'Jack', 'Steve', 'Ricky','Bob'], index=['A', 'B', 'C', 'D', 'E']),
    2. 'Age':pd.Series([28,34,29,42], index=['A', 'B', 'C', 'D'])})
    3. df['Math'] = pd.Series([90, 58, 99, 100, 48], index=['A', 'B', 'C', 'D', 'E'])
    4. df['English'] = pd.Series([100, 100, 80, 100, 70], index=['A', 'B', 'C', 'D', 'E'])
    5. df.loc['F'] = ['John', 51, 88, 89]
    6. print(df)
    7. ‘’’
    8. Name Age Math English
    9. A Tom 28.0 90 100
    10. B Jack 34.0 58 100
    11. C Steve 29.0 99 80
    12. D Ricky 42.0 100 100
    13. E Bob NaN 48 70
    14. F John 51.0 88 89
    15. ’‘’

    但是不能使用iloc增加,会提示IndexError: iloc cannot enlarge its target object

    删除数据行

    DataFrame.drop(labels=None*axis=0index=Nonecolumns=Nonelevel=Noneinplace=Falseerrors='raise')

    这个方法可以删除行,也可以删除列,如果未设置inplace,将得到删除数据后的一个新的DataFrame,原数据没有改变。

    参数说明:

    • labels:行、列的标签名,默认是行,和后面的axis配合使用
    • axis:默认是行,如果axis=1,则labels是列标签
    • index:直接指定行标签
    • columns:直接指定列标签
    1. import pandas as pd
    2. df = pd.DataFrame({'Name':pd.Series(['Tom', 'Jack', 'Steve', 'Ricky','Bob'], index=['A', 'B', 'C', 'D', 'E']),
    3. 'Age':pd.Series([28,34,29,42], index=['A', 'B', 'C', 'D'])})
    4. df['Math'] = pd.Series([90, 58, 99, 100, 48], index=['A', 'B', 'C', 'D', 'E'])
    5. df['English'] = pd.Series([100, 100, 80, 100, 70], index=['A', 'B', 'C', 'D', 'E'])
    6. df2 = df.drop(['B','C'], axis=0)
    7. print('df:\n', df, '\ndf2:\n', df2)
    8. ‘’’
    9. df:
    10. Name Age Math English
    11. A Tom 28.0 90 100
    12. B Jack 34.0 58 100
    13. C Steve 29.0 99 80
    14. D Ricky 42.0 100 100
    15. E Bob NaN 48 70
    16. df2:
    17. Name Age Math English
    18. A Tom 28.0 90 100
    19. D Ricky 42.0 100 100
    20. E Bob NaN 48 70
    21. ’‘’
    22. print("df.drop(index=['A', 'D']):\n", df.drop(index=['A', 'D']))
    23. ‘’’
    24. df.drop(index=['A', 'D']):
    25. Name Age Math English
    26. B Jack 34.0 58 100
    27. C Steve 29.0 99 80
    28. E Bob NaN 48 70
    29. ’‘’

    其他常见方法

    方法名属性&方法描述
    T行和列转置。
    axes返回一个仅以行轴标签和列轴标签为成员的列表。
    dtypes返回每列数据的数据类型。
    emptyDataFrame中没有数据或者任意坐标轴的长度为0,则返回True。
    ndim轴的数量,也指数组的维数。
    shape返回一个元组,表示了 DataFrame 维度。
    sizeDataFrame中的元素数量。
    values使用 numpy 数组表示 DataFrame 中的元素值。
    head()返回前 n 行数据。
    tail()返回后 n 行数据。
    shift()将行或列移动指定的步幅长度

  • 相关阅读:
    分析性质+排列置换环+最小割:1024T4
    微信小程序抓包-夜神模拟器结合BurpSuite抓包(可用于现在最新版本微信)
    【ECMAScript6】模块化
    Vue.js(2): 组件与路由基础指南
    Guitar Pro 8win10最新版吉他学习 / 打谱 / 创作
    函数指针、函数指针数组、计算器+转移表等归纳总结
    无法启动此程序win10玩游戏找不到d3dx9_43.dll缺失的五种常用有效解决方法
    Vc - Qt - “扩张“的窗口
    计算机操作系统-并发控制
    MySQL索引、事务、事务与存储引擎
  • 原文地址:https://blog.csdn.net/spiritx/article/details/133618378