Pandas案例

90个Pandas案例。

  • 如何使用列表和字典创建 Series

    • 使用列表创建 Series

    • 使用 name 参数创建 Series

    • 使用简写的列表创建 Series

    • 使用字典创建 Series

  • 如何使用 Numpy 函数创建 Series

  • 如何获取 Series 的索引和值

  • 如何在创建 Series 时指定索引

  • 如何获取 Series 的大小和形状

  • 如何获取 Series 开始或末尾几行数据

    • Head()

    • Tail()

    • Take()

  • 使用切片获取 Series 子集

  • 如何创建 DataFrame

  • 如何设置 DataFrame 的索引和列信息

  • 如何重命名 DataFrame 的列名称

  • 如何根据 Pandas 列中的值从 DataFrame 中选择或过滤行

  • 在 DataFrame 中使用“isin”过滤多行

  • 迭代 DataFrame 的行和列

  • 如何通过名称或索引删除 DataFrame 的列

  • 向 DataFrame 中新增列

  • 如何从 DataFrame 中获取列标题列表

  • 如何随机生成 DataFrame

  • 如何选择 DataFrame 的多个列

  • 如何将字典转换为 DataFrame

  • 使用 ioc 进行切片

  • 检查 DataFrame 中是否是空的

  • 在创建 DataFrame 时指定索引和列名称

  • 使用 iloc 进行切片

  • iloc 和 loc 的区别

  • 使用时间索引创建空 DataFrame

  • 如何改变 DataFrame 列的排序

  • 检查 DataFrame 列的数据类型

  • 更改 DataFrame 指定列的数据类型

  • 如何将列的数据类型转换为 DateTime 类型

  • 将 DataFrame 列从 floats 转为 ints

  • 如何把 dates 列转换为 DateTime 类型

  • 两个 DataFrame 相加

  • 在 DataFrame 末尾添加额外的行

  • 为指定索引添加新行

  • 如何使用 for 循环添加行

  • 在 DataFrame 顶部添加一行

  • 如何向 DataFrame 中动态添加行

  • 在任意位置插入行

  • 使用时间戳索引向 DataFrame 中添加行

  • 为不同的行填充缺失值

  • append, concat 和 combine_first 示例

  • 获取行和列的平均值

  • 计算行和列的总和

  • 连接两列

  • 过滤包含某字符串的行

  • 过滤索引中包含某字符串的行

  • 使用 AND 运算符过滤包含特定字符串值的行

  • 查找包含某字符串的所有行

  • 如果行中的值包含字符串,则创建与字符串相等的另一列

  • 计算 pandas group 中每组的行数

  • 检查字符串是否在 DataFrme 中

  • 从 DataFrame 列中获取唯一行值

  • 计算 DataFrame 列的不同值

  • 删除具有重复索引的行

  • 删除某些列具有重复值的行

  • 从 DataFrame 单元格中获取值

  • 使用 DataFrame 中的条件索引获取单元格上的标量值

  • 设置 DataFrame 的特定单元格值

  • 从 DataFrame 行获取单元格值

  • 用字典替换 DataFrame 列中的值

  • 统计基于某一列的一列的数值

  • 处理 DataFrame 中的缺失值

  • 删除包含任何缺失数据的行

  • 删除 DataFrame 中缺失数据的列

  • 按降序对索引值进行排序

  • 按降序对列进行排序

  • 使用 rank 方法查找 DataFrame 中元素的排名

  • 在多列上设置索引

  • 确定 DataFrame 的周期索引和列

  • 导入 CSV 指定特定索引

  • 将 DataFrame 写入 csv

  • 使用 Pandas 读取 csv 文件的特定列

  • Pandas 获取 CSV 列的列表

  • 找到列值最大的行

  • 使用查询方法进行复杂条件选择

  • 检查 Pandas 中是否存在列

  • 为特定列从 DataFrame 中查找 n-smallest 和 n-largest 值

  • 从 DataFrame 中查找所有列的最小值和最大值

  • 在 DataFrame 中找到最小值和最大值所在的索引位置

  • 计算 DataFrame Columns 的累积乘积和累积总和

  • 汇总统计

  • 查找 DataFrame 的均值、中值和众数

  • 测量 DataFrame 列的方差和标准偏差

  • 计算 DataFrame 列之间的协方差

  • 计算 Pandas 中两个 DataFrame 对象之间的相关性

  • 计算 DataFrame 列的每个单元格的百分比变化

  • 在 Pandas 中向前和向后填充 DataFrame 列的缺失值

  • 在 Pandas 中使用非分层索引使用 Stacking

  • 使用分层索引对 Pandas 进行拆分

  • Pandas 获取 HTML 页面上 table 数据

1如何使用列表和字典创建 Series

使用列表创建 Series

  1.  
    import pandas as pd
  2.  
     
  3.  
    ser1 = pd.Series([1.52.534.55.06])
  4.  
    print(ser1)

Output:

  1.  
    0 1.5
  2.  
    1 2.5
  3.  
    2 3.0
  4.  
    3 4.5
  5.  
    4 5.0
  6.  
    5 6.0
  7.  
    dtype: float64

使用 name 参数创建 Series

  1.  
    import pandas as pd
  2.  
     
  3.  
    ser2 = pd.Series(["India""Canada""Germany"], name="Countries")
  4.  
    print(ser2)

Output:

  1.  
    0 India
  2.  
    1 Canada
  3.  
    2 Germany
  4.  
    Name: Countries, dtype: object

使用简写的列表创建 Series

  1.  
    import pandas as pd
  2.  
     
  3.  
    ser3 = pd.Series(["A"]*4)
  4.  
    print(ser3)

Output:

  1.  
    0 A
  2.  
    1 A
  3.  
    2 A
  4.  
    3 A
  5.  
    dtype: object

使用字典创建 Series

  1.  
    import pandas as pd
  2.  
     
  3.  
    ser4 = pd.Series({"India""New Delhi",
  4.  
                      "Japan""Tokyo",
  5.  
                      "UK""London"})
  6.  
    print(ser4)

Output:

  1.  
    India New Delhi
  2.  
    Japan Tokyo
  3.  
    UK London
  4.  
    dtype: object

2如何使用 Numpy 函数创建 Series

  1.  
    import pandas as pd
  2.  
    import numpy as np
  3.  
     
  4.  
    ser1 = pd.Series(np.linspace(1105))
  5.  
    print(ser1)
  6.  
     
  7.  
    ser2 = pd.Series(np.random.normal(size=5))
  8.  
    print(ser2)

Output:

  1.  
    0 1.00
  2.  
    1 3.25
  3.  
    2 5.50
  4.  
    3 7.75
  5.  
    4 10.00
  6.  
    dtype: float64
  7.  
    0 -1.694452
  8.  
    1 -1.570006
  9.  
    2 1.713794
  10.  
    3 0.338292
  11.  
    4 0.803511
  12.  
    dtype: float64

3如何获取 Series 的索引和值

  1.  
    import pandas as pd
  2.  
    import numpy as np
  3.  
     
  4.  
    ser1 = pd.Series({"India""New Delhi",
  5.  
                      "Japan""Tokyo",
  6.  
                      "UK""London"})
  7.  
     
  8.  
    print(ser1.values)
  9.  
    print(ser1.index)
  10.  
     
  11.  
    print("\n")
  12.  
     
  13.  
    ser2 = pd.Series(np.random.normal(size=5))
  14.  
    print(ser2.index)
  15.  
    print(ser2.values)

Output:

  1.  
    ['New Delhi' 'Tokyo' 'London']
  2.  
    Index(['India', 'Japan', 'UK'], dtype='object')
  3.  
     
  4.  
     
  5.  
    RangeIndex(start=0, stop=5, step=1)
  6.  
    [ 0.66265478 -0.72222211 0.3608642 1.40955436 1.3096732 ]

4如何在创建 Series 时指定索引

  1.  
    import pandas as pd
  2.  
     
  3.  
    values = ["India""Canada""Australia",
  4.  
              "Japan""Germany""France"]
  5.  
     
  6.  
    code = ["IND""CAN""AUS""JAP""GER""FRA"]
  7.  
     
  8.  
    ser1 = pd.Series(values, index=code)
  9.  
     
  10.  
    print(ser1)

Output:

  1.  
    IND India
  2.  
    CAN Canada
  3.  
    AUS Australia
  4.  
    JAP Japan
  5.  
    GER Germany
  6.  
    FRA France
  7.  
    dtype: object

5如何获取 Series 的大小和形状

  1.  
    import pandas as pd
  2.  
     
  3.  
    values = ["India""Canada""Australia",
  4.  
              "Japan""Germany""France"]
  5.  
     
  6.  
    code = ["IND""CAN""AUS""JAP""GER""FRA"]
  7.  
     
  8.  
    ser1 = pd.Series(values, index=code)
  9.  
     
  10.  
    print(len(ser1))
  11.  
     
  12.  
    print(ser1.shape)
  13.  
     
  14.  
    print(ser1.size)

Output:

  1.  
    6
  2.  
    (6,)
  3.  
    6

6如何获取 Series 开始或末尾几行数据

Head()

  1.  
    import pandas as pd
  2.  
     
  3.  
    values = ["India""Canada""Australia",
  4.  
              "Japan""Germany""France"]
  5.  
     
  6.  
    code = ["IND""CAN""AUS""JAP""GER""FRA"]
  7.  
     
  8.  
    ser1 = pd.Series(values, index=code)
  9.  
     
  10.  
    print("-----Head()-----")
  11.  
    print(ser1.head())
  12.  
     
  13.  
    print("\n\n-----Head(2)-----")
  14.  
    print(ser1.head(2))

Output:

  1.  
    -----Head()-----
  2.  
    IND India
  3.  
    CAN Canada
  4.  
    AUS Australia
  5.  
    JAP Japan
  6.  
    GER Germany
  7.  
    dtype: object
  8.  
     
  9.  
     
  10.  
    -----Head(2)-----
  11.  
    IND India
  12.  
    CAN Canada
  13.  
    dtype: object

Tail()

  1.  
    import pandas as pd
  2.  
     
  3.  
    values = ["India""Canada""Australia",
  4.  
              "Japan""Germany""France"]
  5.  
     
  6.  
    code = ["IND""CAN""AUS""JAP""GER""FRA"]
  7.  
     
  8.  
    ser1 = pd.Series(values, index=code)
  9.  
     
  10.  
    print("-----Tail()-----")
  11.  
    print(ser1.tail())
  12.  
     
  13.  
    print("\n\n-----Tail(2)-----")
  14.  
    print(ser1.tail(2))

Output:

  1.  
    -----Tail()-----
  2.  
    CAN Canada
  3.  
    AUS Australia
  4.  
    JAP Japan
  5.  
    GER Germany
  6.  
    FRA France
  7.  
    dtype: object
  8.  
     
  9.  
     
  10.  
    -----Tail(2)-----
  11.  
    GER Germany
  12.  
    FRA France
  13.  
    dtype: object

Take()

  1.  
    import pandas as pd
  2.  
     
  3.  
    values = ["India""Canada""Australia",
  4.  
              "Japan""Germany""France"]
  5.  
     
  6.  
    code = ["IND""CAN""AUS""JAP""GER""FRA"]
  7.  
     
  8.  
    ser1 = pd.Series(values, index=code)
  9.  
     
  10.  
    print("-----Take()-----")
  11.  
    print(ser1.take([245]))

Output:

  1.  
    -----Take()-----
  2.  
    AUS Australia
  3.  
    GER Germany
  4.  
    FRA France
  5.  
    dtype: object

7使用切片获取 Series 子集

  1.  
    import pandas as pd
  2.  
     
  3.  
    num = [000, 100, 200, 300, 400, 500, 600, 700, 800, 900]
  4.  
     
  5.  
    idx = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
  6.  
     
  7.  
    series = pd.Series(num, index=idx)
  8.  
     
  9.  
    print("\n [2:2] \n")
  10.  
    print(series[2:4])
  11.  
     
  12.  
    print("\n [1:6:2] \n")
  13.  
    print(series[1:6:2])
  14.  
     
  15.  
    print("\n [:6] \n")
  16.  
    print(series[:6])
  17.  
     
  18.  
    print("\n [4:] \n")
  19.  
    print(series[4:])
  20.  
     
  21.  
    print("\n [:4:2] \n")
  22.  
    print(series[:4:2])
  23.  
     
  24.  
    print("\n [4::2] \n")
  25.  
    print(series[4::2])
  26.  
     
  27.  
    print("\n [::-1] \n")
  28.  
    print(series[::-1])

Output

  1.  
    [2:2]
  2.  
     
  3.  
    C 200
  4.  
    D 300
  5.  
    dtype: int64
  6.  
     
  7.  
    [1:6:2]
  8.  
     
  9.  
    B 100
  10.  
    D 300
  11.  
    F 500
  12.  
    dtype: int64
  13.  
     
  14.  
    [:6]
  15.  
     
  16.  
    A 0
  17.  
    B 100
  18.  
    C 200
  19.  
    D 300
  20.  
    E 400
  21.  
    F 500
  22.  
    dtype: int64
  23.  
     
  24.  
    [4:]
  25.  
     
  26.  
    E 400
  27.  
    F 500
  28.  
    G 600
  29.  
    H 700
  30.  
    I 800
  31.  
    J 900
  32.  
    dtype: int64
  33.  
     
  34.  
    [:4:2]
  35.  
     
  36.  
    A 0
  37.  
    C 200
  38.  
    dtype: int64
  39.  
     
  40.  
    [4::2]
  41.  
     
  42.  
    E 400
  43.  
    G 600
  44.  
    I 800
  45.  
    dtype: int64
  46.  
     
  47.  
    [::-1]
  48.  
     
  49.  
    J 900
  50.  
    I 800
  51.  
    H 700
  52.  
    G 600
  53.  
    F 500
  54.  
    E 400
  55.  
    D 300
  56.  
    C 200
  57.  
    B 100
  58.  
    A 0
  59.  
    dtype: int64

8如何创建 DataFrame

  1.  
    import pandas as pd
  2.  
     
  3.  
    employees = pd.DataFrame({
  4.  
    'EmpCode': ['Emp001', 'Emp00'],
  5.  
    'Name': ['John Doe', 'William Spark'],
  6.  
    'Occupation': ['Chemist', 'Statistician'],
  7.  
    'Date Of Join': ['2018-01-25', '2018-01-26'],
  8.  
    'Age': [23, 24]})
  9.  
     
  10.  
    print(employees)

Output:

  1.  
    Age Date Of Join EmpCode Name Occupation
  2.  
    0 23 2018-01-25 Emp001 John Doe Chemist
  3.  
    1 24 2018-01-26 Emp00 William Spark Statistician

9如何设置 DataFrame 的索引和列信息

  1.  
    import pandas as pd
  2.  
     
  3.  
    employees = pd.DataFrame(
  4.  
        data={'Name': ['John Doe''William Spark'],
  5.  
              'Occupation': ['Chemist''Statistician'],
  6.  
              'Date Of Join': ['2018-01-25''2018-01-26'],
  7.  
              'Age': [2324]},
  8.  
        index=['Emp001''Emp002'],
  9.  
        columns=['Name''Occupation''Date Of Join''Age'])
  10.  
     
  11.  
    print(employees)

Output

  1.  
    Name Occupation Date Of Join Age
  2.  
    Emp001 John Doe Chemist 2018-01-25 23
  3.  
    Emp002 William Spark Statistician 2018-01-26 24

10如何重命名 DataFrame 的列名称

  1.  
    import pandas as pd
  2.  
     
  3.  
    employees = pd.DataFrame({
  4.  
        'EmpCode': ['Emp001''Emp00'],
  5.  
        'Name': ['John Doe''William Spark'],
  6.  
        'Occupation': ['Chemist''Statistician'],
  7.  
        'Date Of Join': ['2018-01-25''2018-01-26'],
  8.  
        'Age': [2324]})
  9.  
     
  10.  
    employees.columns = ['EmpCode''EmpName''EmpOccupation''EmpDOJ''EmpAge']
  11.  
     
  12.  
    print(employees)

Output:

  1.  
    EmpCode EmpName EmpOccupation EmpDOJ EmpAge
  2.  
    0 23 2018-01-25 Emp001 John Doe Chemist
  3.  
    1 24 2018-01-26 Emp00 William Spark Statistician

11如何根据 Pandas 列中的值从 DataFrame 中选择或过滤行

  1.  
    import pandas as pd
  2.  
     
  3.  
    employees = pd.DataFrame({
  4.  
        'EmpCode': ['Emp001''Emp002''Emp003''Emp004''Emp005'],
  5.  
        'Name': ['John''Doe''William''Spark''Mark'],
  6.  
        'Occupation': ['Chemist''Statistician''Statistician',
  7.  
                       'Statistician''Programmer'],
  8.  
        'Date Of Join': ['2018-01-25''2018-01-26''2018-01-26''2018-02-26',
  9.  
                         '2018-03-16'],
  10.  
        'Age': [2324342940]})
  11.  
     
  12.  
    print("\nUse == operator\n")
  13.  
    print(employees.loc[employees['Age'] == 23])
  14.  
     
  15.  
    print("\nUse < operator\n")
  16.  
    print(employees.loc[employees['Age'] < 30])
  17.  
     
  18.  
    print("\nUse != operator\n")
  19.  
    print(employees.loc[employees['Occupation'] != 'Statistician'])
  20.  
     
  21.  
    print("\nMultiple Conditions\n")
  22.  
    print(employees.loc[(employees['Occupation'] != 'Statistician') &
  23.  
                        (employees['Name'] == 'John')])

Output:

  1.  
    Use == operator
  2.  
     
  3.  
    Age Date Of Join EmpCode Name Occupation
  4.  
    0 23 2018-01-25 Emp001 John Chemist
  5.  
     
  6.  
    Use < operator
  7.  
     
  8.  
    Age Date Of Join EmpCode Name Occupation
  9.  
    0 23 2018-01-25 Emp001 John Chemist
  10.  
    1 24 2018-01-26 Emp002 Doe Statistician
  11.  
    3 29 2018-02-26 Emp004 Spark Statistician
  12.  
     
  13.  
    Use != operator
  14.  
     
  15.  
    Age Date Of Join EmpCode Name Occupation
  16.  
    0 23 2018-01-25 Emp001 John Chemist
  17.  
    4 40 2018-03-16 Emp005 Mark Programmer
  18.  
     
  19.  
    Multiple Conditions
  20.  
     
  21.  
    Age Date Of Join EmpCode Name Occupation
  22.  
    0 23 2018-01-25 Emp001 John Chemist

12在 DataFrame 中使用“isin”过滤多行

  1.  
    import pandas as pd
  2.  
     
  3.  
    employees = pd.DataFrame({
  4.  
        'EmpCode': ['Emp001''Emp002''Emp003''Emp004''Emp005'],
  5.  
        'Name': ['John''Doe''William''Spark''Mark'],
  6.  
        'Occupation': ['Chemist''Statistician''Statistician',
  7.  
                       'Statistician''Programmer'],
  8.  
        'Date Of Join': ['2018-01-25''2018-01-26''2018-01-26''2018-02-26',
  9.  
                         '2018-03-16'],
  10.  
        'Age': [2324342940]})
  11.  
     
  12.  
    print("\nUse isin operator\n")
  13.  
    print(employees.loc[employees['Occupation'].isin(['Chemist','Programmer'])])
  14.  
     
  15.  
    print("\nMultiple Conditions\n")
  16.  
    print(employees.loc[(employees['Occupation'] == 'Chemist') |
  17.  
                        (employees['Name'] == 'John') &
  18.  
                        (employees['Age'] < 30)])

Output:

  1.  
    Use isin operator
  2.  
     
  3.  
    Age Date Of Join EmpCode Name Occupation
  4.  
    0 23 2018-01-25 Emp001 John Chemist
  5.  
    4 40 2018-03-16 Emp005 Mark Programmer
  6.  
     
  7.  
    Multiple Conditions
  8.  
     
  9.  
    Age Date Of Join EmpCode Name Occupation
  10.  
    0 23 2018-01-25 Emp001 John Chemist

13迭代 DataFrame 的行和列

  1.  
    import pandas as pd
  2.  
     
  3.  
    employees = pd.DataFrame({
  4.  
        'EmpCode': ['Emp001''Emp002''Emp003''Emp004''Emp005'],
  5.  
        'Name': ['John''Doe''William''Spark''Mark'],
  6.  
        'Occupation': ['Chemist''Statistician''Statistician',
  7.  
                       'Statistician''Programmer'],
  8.  
        'Date Of Join': ['2018-01-25''2018-01-26''2018-01-26''2018-02-26',
  9.  
                         '2018-03-16'],
  10.  
        'Age': [2324342940]})
  11.  
     
  12.  
    print("\n Example iterrows \n")
  13.  
    for index, col in employees.iterrows():
  14.  
        print(col['Name'], "--", col['Age'])
  15.  
     
  16.  
     
  17.  
    print("\n Example itertuples \n")
  18.  
    for row in employees.itertuples(index=True, name='Pandas'):
  19.  
        print(getattr(row, "Name"), "--", getattr(row, "Age"))

Output:

  1.  
    Example iterrows
  2.  
     
  3.  
    John -- 23
  4.  
    Doe -- 24
  5.  
    William -- 34
  6.  
    Spark -- 29
  7.  
    Mark -- 40
  8.  
     
  9.  
    Example itertuples
  10.  
     
  11.  
    John -- 23
  12.  
    Doe -- 24
  13.  
    William -- 34
  14.  
    Spark -- 29
  15.  
    Mark -- 40

14如何通过名称或索引删除 DataFrame 的列

  1.  
    import pandas as pd
  2.  
     
  3.  
    employees = pd.DataFrame({
  4.  
        'EmpCode': ['Emp001''Emp002''Emp003''Emp004''Emp005'],
  5.  
        'Name': ['John''Doe''William''Spark''Mark'],
  6.  
        'Occupation': ['Chemist''Statistician''Statistician',
  7.  
                       'Statistician''Programmer'],
  8.  
        'Date Of Join': ['2018-01-25''2018-01-26''2018-01-26''2018-02-26',
  9.  
                         '2018-03-16'],
  10.  
        'Age': [2324342940]})
  11.  
     
  12.  
    print(employees)
  13.  
     
  14.  
    print("\n Drop Column by Name \n")
  15.  
    employees.drop('Age', axis=1, inplace=True)
  16.  
    print(employees)
  17.  
     
  18.  
    print("\n Drop Column by Index \n")
  19.  
    employees.drop(employees.columns[[0,1]], axis=1, inplace=True)
  20.  
    print(employees)

Output:

  1.  
    Age Date Of Join EmpCode Name Occupation
  2.  
    0 23 2018-01-25 Emp001 John Chemist
  3.  
    1 24 2018-01-26 Emp002 Doe Statistician
  4.  
    2 34 2018-01-26 Emp003 William Statistician
  5.  
    3 29 2018-02-26 Emp004 Spark Statistician
  6.  
    4 40 2018-03-16 Emp005 Mark Programmer
  7.  
     
  8.  
    Drop Column by Name
  9.  
     
  10.  
    Date Of Join EmpCode Name Occupation
  11.  
    0 2018-01-25 Emp001 John Chemist
  12.  
    1 2018-01-26 Emp002 Doe Statistician
  13.  
    2 2018-01-26 Emp003 William Statistician
  14.  
    3 2018-02-26 Emp004 Spark Statistician
  15.  
    4 2018-03-16 Emp005 Mark Programmer
  16.  
     
  17.  
    Drop Column by Index
  18.  
     
  19.  
    Name Occupation
  20.  
    0 John Chemist
  21.  
    1 Doe Statistician
  22.  
    2 William Statistician
  23.  
    3 Spark Statistician
  24.  
    4 Mark Programmer

15向 DataFrame 中新增列

  1.  
    import pandas as pd
  2.  
     
  3.  
    employees = pd.DataFrame({
  4.  
        'EmpCode': ['Emp001''Emp002''Emp003''Emp004''Emp005'],
  5.  
        'Name': ['John''Doe''William''Spark''Mark'],
  6.  
        'Occupation': ['Chemist''Statistician''Statistician',
  7.  
                       'Statistician''Programmer'],
  8.  
        'Date Of Join': ['2018-01-25''2018-01-26''2018-01-26''2018-02-26',
  9.  
                         '2018-03-16'],
  10.  
        'Age': [2324342940]})
  11.  
     
  12.  
    employees['City'] = ['London''Tokyo''Sydney''London''Toronto']
  13.  
     
  14.  
    print(employees)

Output:

  1.  
    Age Date Of Join EmpCode Name Occupation City
  2.  
    0 23 2018-01-25 Emp001 John Chemist London
  3.  
    1 24 2018-01-26 Emp002 Doe Statistician Tokyo
  4.  
    2 34 2018-01-26 Emp003 William Statistician Sydney
  5.  
    3 29 2018-02-26 Emp004 Spark Statistician London
  6.  
    4 40 2018-03-16 Emp005 Mark Programmer Toronto

16如何从 DataFrame 中获取列标题列表

  1.  
    import pandas as pd
  2.  
     
  3.  
    employees = pd.DataFrame({
  4.  
        'EmpCode': ['Emp001''Emp002''Emp003''Emp004''Emp005'],
  5.  
        'Name': ['John''Doe''William''Spark''Mark'],
  6.  
        'Occupation': ['Chemist''Statistician''Statistician',
  7.  
                       'Statistician''Programmer'],
  8.  
        'Date Of Join': ['2018-01-25''2018-01-26''2018-01-26''2018-02-26',
  9.  
                         '2018-03-16'],
  10.  
        'Age': [2324342940]})
  11.  
     
  12.  
    print(list(employees))
  13.  
     
  14.  
    print(list(employees.columns.values))
  15.  
     
  16.  
    print(employees.columns.tolist())

Output:

  1.  
    ['Age', 'Date Of Join', 'EmpCode', 'Name', 'Occupation']
  2.  
    ['Age', 'Date Of Join', 'EmpCode', 'Name', 'Occupation']
  3.  
    ['Age', 'Date Of Join', 'EmpCode', 'Name', 'Occupation']

17如何随机生成 DataFrame

  1.  
    import pandas as pd
  2.  
    import numpy as np
  3.  
     
  4.  
    np.random.seed(5)
  5.  
     
  6.  
    df_random = pd.DataFrame(np.random.randint(100, size=(106)),
  7.  
                             columns=list('ABCDEF'),
  8.  
                             index=['Row-{}'.format(i) for i in range(10)])
  9.  
     
  10.  
    print(df_random)

Output:

  1.  
    A B C D E F
  2.  
    Row-0 99 78 61 16 73 8
  3.  
    Row-1 62 27 30 80 7 76
  4.  
    Row-2 15 53 80 27 44 77
  5.  
    Row-3 75 65 47 30 84 86
  6.  
    Row-4 18 9 41 62 1 82
  7.  
    Row-5 16 78 5 58 0 80
  8.  
    Row-6 4 36 51 27 31 2
  9.  
    Row-7 68 38 83 19 18 7
  10.  
    Row-8 30 62 11 67 65 55
  11.  
    Row-9 3 91 78 27 29 33

18如何选择 DataFrame 的多个列

  1.  
    import pandas as pd
  2.  
     
  3.  
    employees = pd.DataFrame({
  4.  
        'EmpCode': ['Emp001''Emp002''Emp003''Emp004''Emp005'],
  5.  
        'Name': ['John''Doe''William''Spark''Mark'],
  6.  
        'Occupation': ['Chemist''Statistician''Statistician',
  7.  
                       'Statistician''Programmer'],
  8.  
        'Date Of Join': ['2018-01-25''2018-01-26''2018-01-26''2018-02-26',
  9.  
                         '2018-03-16'],
  10.  
        'Age': [2324342940]})
  11.  
     
  12.  
    df = employees[['EmpCode''Age''Name']]
  13.  
    print(df)

Output:

  1.  
    EmpCode Age Name
  2.  
    0 Emp001 23 John
  3.  
    1 Emp002 24 Doe
  4.  
    2 Emp003 34 William
  5.  
    3 Emp004 29 Spark
  6.  
    4 Emp005 40 Mark

19如何将字典转换为 DataFrame

  1.  
    import pandas as pd
  2.  
     
  3.  
    data = ({'Age': [30202240322839],
  4.  
                       'Color': ['Blue''Green''Red''White''Gray''Black',
  5.  
                                 'Red'],
  6.  
                       'Food': ['Steak''Lamb''Mango''Apple''Cheese',
  7.  
                                'Melon''Beans'],
  8.  
                       'Height': [1657012080180172150],
  9.  
                       'Score': [4.68.39.03.31.89.52.2],
  10.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  11.  
                       })
  12.  
    print(data)
  13.  
     
  14.  
    df = pd.DataFrame(data)
  15.  
     
  16.  
    print(df)

Output:

  1.  
    {'Height': [165, 70, 120, 80, 180, 172, 150], 'Food': ['Steak', 'Lamb', 'Mango',
  2.  
    'Apple', 'Cheese', 'Melon', 'Beans'], 'Age': [30, 20, 22, 40, 32, 28, 39], 'Sco
  3.  
    re': [4.6, 8.3, 9.0, 3.3, 1.8, 9.5, 2.2], 'Color': ['Blue', 'Green', 'Red', 'Whi
  4.  
    te', 'Gray', 'Black', 'Red'], 'State': ['NY', 'TX', 'FL', 'AL', 'AK', 'TX', 'TX'
  5.  
    ]}
  6.  
    Age Color Food Height Score State
  7.  
    0 30 Blue Steak 165 4.6 NY
  8.  
    1 20 Green Lamb 70 8.3 TX
  9.  
    2 22 Red Mango 120 9.0 FL
  10.  
    3 40 White Apple 80 3.3 AL
  11.  
    4 32 Gray Cheese 180 1.8 AK
  12.  
    5 28 Black Melon 172 9.5 TX
  13.  
    6 39 Red Beans 150 2.2 TX

20使用 ioc 进行切片

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'Age': [30202240322839],
  4.  
                       'Color': ['Blue''Green''Red''White''Gray''Black',
  5.  
                                 'Red'],
  6.  
                       'Food': ['Steak''Lamb''Mango''Apple''Cheese',
  7.  
                                'Melon''Beans'],
  8.  
                       'Height': [1657012080180172150],
  9.  
                       'Score': [4.68.39.03.31.89.52.2],
  10.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  11.  
                       },
  12.  
                      index=['Jane''Nick''Aaron''Penelope''Dean',
  13.  
                             'Christina''Cornelia'])
  14.  
     
  15.  
    print("\n -- Selecting a single row with .loc with a string -- \n")
  16.  
    print(df.loc['Penelope'])
  17.  
     
  18.  
    print("\n -- Selecting multiple rows with .loc with a list of strings -- \n")
  19.  
    print(df.loc[['Cornelia''Jane''Dean']])
  20.  
     
  21.  
    print("\n -- Selecting multiple rows with .loc with slice notation -- \n")
  22.  
    print(df.loc['Aaron':'Dean'])

Output:

  1.  
    -- Selecting a single row with .loc with a string --
  2.  
     
  3.  
    Age 40
  4.  
    Color White
  5.  
    Food Apple
  6.  
    Height 80
  7.  
    Score 3.3
  8.  
    State AL
  9.  
    Name: Penelope, dtype: object
  10.  
     
  11.  
    -- Selecting multiple rows with .loc with a list of strings --
  12.  
     
  13.  
    Age Color Food Height Score State
  14.  
    Cornelia 39 Red Beans 150 2.2 TX
  15.  
    Jane 30 Blue Steak 165 4.6 NY
  16.  
    Dean 32 Gray Cheese 180 1.8 AK
  17.  
     
  18.  
    -- Selecting multiple rows with .loc with slice notation --
  19.  
     
  20.  
    Age Color Food Height Score State
  21.  
    Aaron 22 Red Mango 120 9.0 FL
  22.  
    Penelope 40 White Apple 80 3.3 AL
  23.  
    Dean 32 Gray Cheese 180 1.8 AK

21检查 DataFrame 中是否是空的

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame()
  4.  
     
  5.  
    if df.empty:
  6.  
        print('DataFrame is empty!')

Output:

DataFrame is empty!
 

22在创建 DataFrame 时指定索引和列名称

  1.  
    import pandas as pd
  2.  
     
  3.  
    values = ["India""Canada""Australia",
  4.  
              "Japan""Germany""France"]
  5.  
     
  6.  
    code = ["IND""CAN""AUS""JAP""GER""FRA"]
  7.  
     
  8.  
    df = pd.DataFrame(values, index=code, columns=['Country'])
  9.  
     
  10.  
    print(df)

Output:

  1.  
    Country
  2.  
    IND India
  3.  
    CAN Canada
  4.  
    AUS Australia
  5.  
    JAP Japan
  6.  
    GER Germany
  7.  
    FRA France

23使用 iloc 进行切片

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'Age': [30202240322839],
  4.  
                       'Color': ['Blue''Green''Red''White''Gray''Black',
  5.  
                                 'Red'],
  6.  
                       'Food': ['Steak''Lamb''Mango''Apple''Cheese',
  7.  
                                'Melon''Beans'],
  8.  
                       'Height': [1657012080180172150],
  9.  
                       'Score': [4.68.39.03.31.89.52.2],
  10.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  11.  
                       },
  12.  
                      index=['Jane''Nick''Aaron''Penelope''Dean',
  13.  
                             'Christina''Cornelia'])
  14.  
     
  15.  
    print("\n -- Selecting a single row with .iloc with an integer -- \n")
  16.  
    print(df.iloc[4])
  17.  
     
  18.  
    print("\n -- Selecting multiple rows with .iloc with a list of integers -- \n")
  19.  
    print(df.iloc[[2-2]])
  20.  
     
  21.  
    print("\n -- Selecting multiple rows with .iloc with slice notation -- \n")
  22.  
    print(df.iloc[:5:3])

Output:

  1.  
    -- Selecting a single row with .iloc with an integer --
  2.  
     
  3.  
    Age 32
  4.  
    Color Gray
  5.  
    Food Cheese
  6.  
    Height 180
  7.  
    Score 1.8
  8.  
    State AK
  9.  
    Name: Dean, dtype: object
  10.  
     
  11.  
    -- Selecting multiple rows with .iloc with a list of integers --
  12.  
     
  13.  
    Age Color Food Height Score State
  14.  
    Aaron 22 Red Mango 120 9.0 FL
  15.  
    Christina 28 Black Melon 172 9.5 TX
  16.  
     
  17.  
    -- Selecting multiple rows with .iloc with slice notation --
  18.  
     
  19.  
    Age Color Food Height Score State
  20.  
    Jane 30 Blue Steak 165 4.6 NY
  21.  
    Penelope 40 White Apple 80 3.3 AL

24iloc 和 loc 的区别

  • loc 索引器还可以进行布尔选择,例如,如果我们想查找 Age 小于 30 的所有行并仅返回 Color 和 Height 列,我们可以执行以下操作。我们可以用 iloc 复制它,但我们不能将它传递给一个布尔系列,必须将布尔系列转换为 numpy 数组

  • loc 从索引中获取具有特定标签的行(或列)

  • iloc 在索引中的特定位置获取行(或列)(因此它只需要整数)

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'Age': [30202240322839],
  4.  
                       'Color': ['Blue''Green''Red''White''Gray''Black',
  5.  
                                 'Red'],
  6.  
                       'Food': ['Steak''Lamb''Mango''Apple''Cheese',
  7.  
                                'Melon''Beans'],
  8.  
                       'Height': [1657012080180172150],
  9.  
                       'Score': [4.68.39.03.31.89.52.2],
  10.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  11.  
                       },
  12.  
                      index=['Jane''Nick''Aaron''Penelope''Dean',
  13.  
                             'Christina''Cornelia'])
  14.  
     
  15.  
    print("\n -- loc -- \n")
  16.  
    print(df.loc[df['Age'] < 30, ['Color''Height']])
  17.  
     
  18.  
    print("\n -- iloc -- \n")
  19.  
    print(df.iloc[(df['Age'] < 30).values, [13]])

Output:

  1.  
    -- loc --
  2.  
     
  3.  
    Color Height
  4.  
    Nick Green 70
  5.  
    Aaron Red 120
  6.  
    Christina Black 172
  7.  
     
  8.  
    -- iloc --
  9.  
     
  10.  
    Color Height
  11.  
    Nick Green 70
  12.  
    Aaron Red 120
  13.  
    Christina Black 172

25使用时间索引创建空 DataFrame

  1.  
    import datetime
  2.  
    import pandas as pd
  3.  
     
  4.  
    todays_date = datetime.datetime.now().date()
  5.  
    index = pd.date_range(todays_date, periods=10, freq='D')
  6.  
     
  7.  
    columns = ['A''B''C']
  8.  
     
  9.  
    df = pd.DataFrame(index=index, columns=columns)
  10.  
    df = df.fillna(0)
  11.  
     
  12.  
    print(df)

Output:

  1.  
    A B C
  2.  
    2018-09-30 0 0 0
  3.  
    2018-10-01 0 0 0
  4.  
    2018-10-02 0 0 0
  5.  
    2018-10-03 0 0 0
  6.  
    2018-10-04 0 0 0
  7.  
    2018-10-05 0 0 0
  8.  
    2018-10-06 0 0 0
  9.  
    2018-10-07 0 0 0
  10.  
    2018-10-08 0 0 0
  11.  
    2018-10-09 0 0 0

26如何改变 DataFrame 列的排序

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'Age': [30202240322839],
  4.  
                       'Color': ['Blue''Green''Red''White''Gray''Black',
  5.  
                                 'Red'],
  6.  
                       'Food': ['Steak''Lamb''Mango''Apple''Cheese',
  7.  
                                'Melon''Beans'],
  8.  
                       'Height': [1657012080180172150],
  9.  
                       'Score': [4.68.39.03.31.89.52.2],
  10.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  11.  
                       },
  12.  
                      index=['Jane''Nick''Aaron''Penelope''Dean',
  13.  
                             'Christina''Cornelia'])
  14.  
     
  15.  
    print("\n -- Change order using columns -- \n")
  16.  
    new_order = [321450]
  17.  
    df = df[df.columns[new_order]]
  18.  
    print(df)
  19.  
     
  20.  
    print("\n -- Change order using reindex -- \n")
  21.  
    df = df.reindex(['State''Color''Age''Food''Score''Height'], axis=1)
  22.  
    print(df)

Output:

  1.  
    -- Change order using columns --
  2.  
     
  3.  
    Height Food Color Score State Age
  4.  
    Jane 165 Steak Blue 4.6 NY 30
  5.  
    Nick 70 Lamb Green 8.3 TX 20
  6.  
    Aaron 120 Mango Red 9.0 FL 22
  7.  
    Penelope 80 Apple White 3.3 AL 40
  8.  
    Dean 180 Cheese Gray 1.8 AK 32
  9.  
    Christina 172 Melon Black 9.5 TX 28
  10.  
    Cornelia 150 Beans Red 2.2 TX 39
  11.  
     
  12.  
    -- Change order using reindex --
  13.  
     
  14.  
    State Color Age Food Score Height
  15.  
    Jane NY Blue 30 Steak 4.6 165
  16.  
    Nick TX Green 20 Lamb 8.3 70
  17.  
    Aaron FL Red 22 Mango 9.0 120
  18.  
    Penelope AL White 40 Apple 3.3 80
  19.  
    Dean AK Gray 32 Cheese 1.8 180
  20.  
    Christina TX Black 28 Melon 9.5 172
  21.  
    Cornelia TX Red 39 Beans 2.2 150

27检查 DataFrame 列的数据类型

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'Age': [30202240322839],
  4.  
                       'Color': ['Blue''Green''Red''White''Gray''Black',
  5.  
                                 'Red'],
  6.  
                       'Food': ['Steak''Lamb''Mango''Apple''Cheese',
  7.  
                                'Melon''Beans'],
  8.  
                       'Height': [1657012080180172150],
  9.  
                       'Score': [4.68.39.03.31.89.52.2],
  10.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  11.  
                       },
  12.  
                      index=['Jane''Nick''Aaron''Penelope''Dean',
  13.  
                             'Christina''Cornelia'])
  14.  
     
  15.  
    print(df.dtypes)

Output:

  1.  
    Age int64
  2.  
    Color object
  3.  
    Food object
  4.  
    Height int64
  5.  
    Score float64
  6.  
    State object
  7.  
    dtype: object

28更改 DataFrame 指定列的数据类型

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'Age': [30202240322839],
  4.  
                       'Color': ['Blue''Green''Red''White''Gray''Black',
  5.  
                                 'Red'],
  6.  
                       'Food': ['Steak''Lamb''Mango''Apple''Cheese',
  7.  
                                'Melon''Beans'],
  8.  
                       'Height': [1657012080180172150],
  9.  
                       'Score': [4.68.39.03.31.89.52.2],
  10.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  11.  
                       },
  12.  
                      index=['Jane''Nick''Aaron''Penelope''Dean',
  13.  
                             'Christina''Cornelia'])
  14.  
     
  15.  
    print(df.dtypes)
  16.  
     
  17.  
    df['Age'] = df['Age'].astype(str)
  18.  
     
  19.  
    print(df.dtypes)

Output:

  1.  
    Age int64
  2.  
    Color object
  3.  
    Food object
  4.  
    Height int64
  5.  
    Score float64
  6.  
    State object
  7.  
    dtype: object
  8.  
    Age object
  9.  
    Color object
  10.  
    Food object
  11.  
    Height int64
  12.  
    Score float64
  13.  
    State object
  14.  
    dtype: object

29如何将列的数据类型转换为 DateTime 类型

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'DateOFBirth': [134972010513498065051349892905,
  4.  
                                       134997930513500657051349792905,
  5.  
                                       1349730105],
  6.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  7.  
                       },
  8.  
                      index=['Jane''Nick''Aaron''Penelope''Dean',
  9.  
                             'Christina''Cornelia'])
  10.  
     
  11.  
    print("\n----------------Before---------------\n")
  12.  
    print(df.dtypes)
  13.  
    print(df)
  14.  
     
  15.  
    df['DateOFBirth'] = pd.to_datetime(df['DateOFBirth'], unit='s')
  16.  
     
  17.  
    print("\n----------------After----------------\n")
  18.  
    print(df.dtypes)
  19.  
    print(df)

Output:

  1.  
    ----------------Before---------------
  2.  
     
  3.  
    DateOFBirth int64
  4.  
    State object
  5.  
    dtype: object
  6.  
    DateOFBirth State
  7.  
    Jane 1349720105 NY
  8.  
    Nick 1349806505 TX
  9.  
    Aaron 1349892905 FL
  10.  
    Penelope 1349979305 AL
  11.  
    Dean 1350065705 AK
  12.  
    Christina 1349792905 TX
  13.  
    Cornelia 1349730105 TX
  14.  
     
  15.  
    ----------------After----------------
  16.  
     
  17.  
    DateOFBirth datetime64[ns]
  18.  
    State object
  19.  
    dtype: object
  20.  
    DateOFBirth State
  21.  
    Jane 2012-10-08 18:15:05 NY
  22.  
    Nick 2012-10-09 18:15:05 TX
  23.  
    Aaron 2012-10-10 18:15:05 FL
  24.  
    Penelope 2012-10-11 18:15:05 AL
  25.  
    Dean 2012-10-12 18:15:05 AK
  26.  
    Christina 2012-10-09 14:28:25 TX
  27.  
    Cornelia 2012-10-08 21:01:45 TX

30将 DataFrame 列从 floats 转为 ints

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'DailyExp': [75.7, 56.69, 55.69, 96.5, 84.9, 110.5,
  4.  
    58.9],
  5.  
    'State': ['NY', 'TX', 'FL', 'AL', 'AK', 'TX', 'TX']
  6.  
    },
  7.  
    index=['Jane', 'Nick', 'Aaron', 'Penelope', 'Dean',
  8.  
    'Christina', 'Cornelia'])
  9.  
     
  10.  
    print("\n----------------Before---------------\n")
  11.  
    print(df.dtypes)
  12.  
    print(df)
  13.  
     
  14.  
    df['DailyExp'] = df['DailyExp'].astype(int)
  15.  
     
  16.  
    print("\n----------------After----------------\n")
  17.  
    print(df.dtypes)
  18.  
    print(df)

Output:

  1.  
    ----------------Before---------------
  2.  
     
  3.  
    DailyExp float64
  4.  
    State object
  5.  
    dtype: object
  6.  
    DailyExp State
  7.  
    Jane 75.70 NY
  8.  
    Nick 56.69 TX
  9.  
    Aaron 55.69 FL
  10.  
    Penelope 96.50 AL
  11.  
    Dean 84.90 AK
  12.  
    Christina 110.50 TX
  13.  
    Cornelia 58.90 TX
  14.  
     
  15.  
    ----------------After----------------
  16.  
     
  17.  
    DailyExp int32
  18.  
    State object
  19.  
    dtype: object
  20.  
    DailyExp State
  21.  
    Jane 75 NY
  22.  
    Nick 56 TX
  23.  
    Aaron 55 FL
  24.  
    Penelope 96 AL
  25.  
    Dean 84 AK
  26.  
    Christina 110 TX
  27.  
    Cornelia 58 TX

31如何把 dates 列转换为 DateTime 类型

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'DateOfBirth': ['1986-11-11''1999-05-12''1976-01-01',
  4.  
                                       '1986-06-01''1983-06-04''1990-03-07',
  5.  
                                       '1999-07-09'],                   
  6.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  7.  
                       },
  8.  
                      index=['Jane''Nick''Aaron''Penelope''Dean',
  9.  
                             'Christina''Cornelia'])
  10.  
     
  11.  
    print("\n----------------Before---------------\n")
  12.  
    print(df.dtypes)
  13.  
      
  14.  
    df['DateOfBirth'] = df['DateOfBirth'].astype('datetime64')
  15.  
      
  16.  
    print("\n----------------After----------------\n")
  17.  
    print(df.dtypes)

Output:

  1.  
    ----------------Before---------------
  2.  
     
  3.  
    DateOfBirth object
  4.  
    State object
  5.  
    dtype: object
  6.  
     
  7.  
    ----------------After----------------
  8.  
     
  9.  
    DateOfBirth datetime64[ns]
  10.  
    State object
  11.  
    dtype: object

32两个 DataFrame 相加

  1.  
    import pandas as pd
  2.  
     
  3.  
    df1 = pd.DataFrame({'Age': [30202240], 'Height': [1657012080],
  4.  
                        'Score': [4.68.39.03.3], 'State': ['NY''TX',
  5.  
                                                                 'FL''AL']},
  6.  
                       index=['Jane''Nick''Aaron''Penelope'])
  7.  
     
  8.  
    df2 = pd.DataFrame({'Age': [322839], 'Color': ['Gray''Black''Red'],
  9.  
                        'Food': ['Cheese''Melon''Beans'],
  10.  
                        'Score': [1.89.52.2], 'State': ['AK''TX''TX']},
  11.  
                       index=['Dean''Christina''Cornelia'])
  12.  
     
  13.  
    df3 = df1.append(df2, sort=True)
  14.  
     
  15.  
    print(df3)

Output:

  1.  
    Age Color Food Height Score State
  2.  
    Jane 30 NaN NaN 165.0 4.6 NY
  3.  
    Nick 20 NaN NaN 70.0 8.3 TX
  4.  
    Aaron 22 NaN NaN 120.0 9.0 FL
  5.  
    Penelope 40 NaN NaN 80.0 3.3 AL
  6.  
    Dean 32 Gray Cheese NaN 1.8 AK
  7.  
    Christina 28 Black Melon NaN 9.5 TX
  8.  
    Cornelia 39 Red Beans NaN 2.2 TX

33在 DataFrame 末尾添加额外的行

  1.  
    import pandas as pd
  2.  
     
  3.  
    employees = pd.DataFrame({
  4.  
        'EmpCode': ['Emp001''Emp002''Emp003''Emp004''Emp005'],
  5.  
        'Name': ['John''Doe''William''Spark''Mark'],
  6.  
        'Occupation': ['Chemist''Statistician''Statistician',
  7.  
                       'Statistician''Programmer'],
  8.  
        'Date Of Join': ['2018-01-25''2018-01-26''2018-01-26''2018-02-26',
  9.  
                         '2018-03-16'],
  10.  
        'Age': [2324342940]})
  11.  
     
  12.  
    print("\n------------ BEFORE ----------------\n")
  13.  
    print(employees)
  14.  
     
  15.  
    employees.loc[len(employees)] = [45'2018-01-25''Emp006''Sunny',
  16.  
                                     'Programmer']
  17.  
     
  18.  
    print("\n------------ AFTER ----------------\n")
  19.  
    print(employees)

Output:

  1.  
    ------------ BEFORE ----------------
  2.  
     
  3.  
    Age Date Of Join EmpCode Name Occupation
  4.  
    0 23 2018-01-25 Emp001 John Chemist
  5.  
    1 24 2018-01-26 Emp002 Doe Statistician
  6.  
    2 34 2018-01-26 Emp003 William Statistician
  7.  
    3 29 2018-02-26 Emp004 Spark Statistician
  8.  
    4 40 2018-03-16 Emp005 Mark Programmer
  9.  
     
  10.  
    ------------ AFTER ----------------
  11.  
     
  12.  
    Age Date Of Join EmpCode Name Occupation
  13.  
    0 23 2018-01-25 Emp001 John Chemist
  14.  
    1 24 2018-01-26 Emp002 Doe Statistician
  15.  
    2 34 2018-01-26 Emp003 William Statistician
  16.  
    3 29 2018-02-26 Emp004 Spark Statistician
  17.  
    4 40 2018-03-16 Emp005 Mark Programmer
  18.  
    5 45 2018-01-25 Emp006 Sunny Programmer

34为指定索引添加新行

  1.  
    import pandas as pd
  2.  
     
  3.  
    employees = pd.DataFrame(
  4.  
        data={'Name': ['John Doe''William Spark'],
  5.  
              'Occupation': ['Chemist''Statistician'],
  6.  
              'Date Of Join': ['2018-01-25''2018-01-26'],
  7.  
              'Age': [2324]},
  8.  
        index=['Emp001''Emp002'],
  9.  
        columns=['Name''Occupation''Date Of Join''Age'])
  10.  
     
  11.  
    print("\n------------ BEFORE ----------------\n")
  12.  
    print(employees)
  13.  
     
  14.  
    employees.loc['Emp003'] = ['Sunny''Programmer''2018-01-25'45]
  15.  
     
  16.  
    print("\n------------ AFTER ----------------\n")
  17.  
    print(employees)

Output:

  1.  
    ------------ BEFORE ----------------
  2.  
     
  3.  
    Name Occupation Date Of Join Age
  4.  
    Emp001 John Doe Chemist 2018-01-25 23
  5.  
    Emp002 William Spark Statistician 2018-01-26 24
  6.  
     
  7.  
    ------------ AFTER ----------------
  8.  
     
  9.  
    Name Occupation Date Of Join Age
  10.  
    Emp001 John Doe Chemist 2018-01-25 23
  11.  
    Emp002 William Spark Statistician 2018-01-26 24
  12.  
    Emp003 Sunny Programmer 2018-01-25 45

35如何使用 for 循环添加行

  1.  
    import pandas as pd
  2.  
     
  3.  
    cols = ['Zip']
  4.  
    lst = []
  5.  
    zip = 32100
  6.  
     
  7.  
    for a in range(10):
  8.  
        lst.append([zip])
  9.  
        zip = zip + 1
  10.  
     
  11.  
    df = pd.DataFrame(lst, columns=cols)
  12.  
     
  13.  
    print(df)

Output:

  1.  
    Zip
  2.  
    0 32100
  3.  
    1 32101
  4.  
    2 32102
  5.  
    3 32103
  6.  
    4 32104
  7.  
    5 32105
  8.  
    6 32106
  9.  
    7 32107
  10.  
    8 32108
  11.  
    9 32109

36在 DataFrame 顶部添加一行

  1.  
    import pandas as pd
  2.  
     
  3.  
    employees = pd.DataFrame({
  4.  
        'EmpCode': ['Emp002''Emp003''Emp004'],
  5.  
        'Name': ['John''Doe''William'],
  6.  
        'Occupation': ['Chemist''Statistician''Statistician'],
  7.  
        'Date Of Join': ['2018-01-25''2018-01-26''2018-01-26'],
  8.  
        'Age': [232434]})
  9.  
     
  10.  
    print("\n------------ BEFORE ----------------\n")
  11.  
    print(employees)
  12.  
     
  13.  
    # New line
  14.  
    line = pd.DataFrame({'Name''Dean''Age'45'EmpCode''Emp001',
  15.  
                         'Date Of Join''2018-02-26''Occupation''Chemist'
  16.  
                         }, index=[0])
  17.  
     
  18.  
    # Concatenate two dataframe
  19.  
    employees = pd.concat([line,employees.ix[:]]).reset_index(drop=True)
  20.  
     
  21.  
    print("\n------------ AFTER ----------------\n")
  22.  
    print(employees)

Output:

  1.  
    ------------ BEFORE ----------------
  2.  
     
  3.  
    Age Date Of Join EmpCode Name Occupation
  4.  
    0 23 2018-01-25 Emp002 John Chemist
  5.  
    1 24 2018-01-26 Emp003 Doe Statistician
  6.  
    2 34 2018-01-26 Emp004 William Statistician
  7.  
     
  8.  
    ------------ AFTER ----------------
  9.  
     
  10.  
    Age Date Of Join EmpCode Name Occupation
  11.  
    0 45 2018-02-26 Emp001 Dean Chemist
  12.  
    1 23 2018-01-25 Emp002 John Chemist
  13.  
    2 24 2018-01-26 Emp003 Doe Statistician
  14.  
    3 34 2018-01-26 Emp004 William Statistician

37如何向 DataFrame 中动态添加行

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame(columns=['Name''Age'])
  4.  
     
  5.  
    df.loc[1'Name'] = 'Rocky'
  6.  
    df.loc[1'Age'] = 23
  7.  
     
  8.  
    df.loc[2'Name'] = 'Sunny'
  9.  
     
  10.  
    print(df)

Output:

  1.  
    Name Age
  2.  
    1 Rocky 23
  3.  
    2 Sunny NaN

38在任意位置插入行

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame(columns=['Name''Age'])
  4.  
     
  5.  
    df.loc[1'Name'] = 'Rocky'
  6.  
    df.loc[1'Age'] = 21
  7.  
     
  8.  
    df.loc[2'Name'] = 'Sunny'
  9.  
    df.loc[2'Age'] = 22
  10.  
     
  11.  
    df.loc[3'Name'] = 'Mark'
  12.  
    df.loc[3'Age'] = 25
  13.  
     
  14.  
    df.loc[4'Name'] = 'Taylor'
  15.  
    df.loc[4'Age'] = 28
  16.  
     
  17.  
    print("\n------------ BEFORE ----------------\n")
  18.  
    print(df)
  19.  
     
  20.  
    line = pd.DataFrame({"Name""Jack""Age"24}, index=[2.5])
  21.  
    df = df.append(line, ignore_index=False)
  22.  
    df = df.sort_index().reset_index(drop=True)
  23.  
     
  24.  
    df = df.reindex(['Name''Age'], axis=1)
  25.  
    print("\n------------ AFTER ----------------\n")
  26.  
    print(df)

Output:

  1.  
    ------------ BEFORE ----------------
  2.  
     
  3.  
    Name Age
  4.  
    1 Rocky 21
  5.  
    2 Sunny 22
  6.  
    3 Mark 25
  7.  
    4 Taylor 28
  8.  
     
  9.  
    ------------ AFTER ----------------
  10.  
     
  11.  
    Name Age
  12.  
    0 Rocky 21
  13.  
    1 Sunny 22
  14.  
    2 Jack 24
  15.  
    3 Mark 25
  16.  
    4 Taylor 28

39使用时间戳索引向 DataFrame 中添加行

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame(columns=['Name''Age'])
  4.  
     
  5.  
    df.loc['2014-05-01 18:47:05''Name'] = 'Rocky'
  6.  
    df.loc['2014-05-01 18:47:05''Age'] = 21
  7.  
     
  8.  
    df.loc['2014-05-02 18:47:05''Name'] = 'Sunny'
  9.  
    df.loc['2014-05-02 18:47:05''Age'] = 22
  10.  
     
  11.  
    df.loc['2014-05-03 18:47:05''Name'] = 'Mark'
  12.  
    df.loc['2014-05-03 18:47:05''Age'] = 25
  13.  
     
  14.  
    print("\n------------ BEFORE ----------------\n")
  15.  
    print(df)
  16.  
     
  17.  
    line = pd.to_datetime("2014-05-01 18:50:05", format="%Y-%m-%d %H:%M:%S")
  18.  
    new_row = pd.DataFrame([['Bunny'26]], columns=['Name''Age'], index=[line])
  19.  
    df = pd.concat([df, pd.DataFrame(new_row)], ignore_index=False)
  20.  
     
  21.  
    print("\n------------ AFTER ----------------\n")
  22.  
    print(df)

Output:

  1.  
    ------------ BEFORE ----------------
  2.  
     
  3.  
    Name Age
  4.  
    2014-05-01 18:47:05 Rocky 21
  5.  
    2014-05-02 18:47:05 Sunny 22
  6.  
    2014-05-03 18:47:05 Mark 25
  7.  
     
  8.  
    ------------ AFTER ----------------
  9.  
     
  10.  
    Name Age
  11.  
    2014-05-01 18:47:05 Rocky 21
  12.  
    2014-05-02 18:47:05 Sunny 22
  13.  
    2014-05-03 18:47:05 Mark 25
  14.  
    2014-05-01 18:50:05 Bunny 26

40为不同的行填充缺失值

  1.  
    import pandas as pd
  2.  
     
  3.  
    a = {'A'10'B'20}
  4.  
    b = {'B'30'C'40'D'50}
  5.  
     
  6.  
    df1 = pd.DataFrame(a, index=[0])
  7.  
    df2 = pd.DataFrame(b, index=[1])
  8.  
     
  9.  
    df = pd.DataFrame()
  10.  
    df = df.append(df1)
  11.  
    df = df.append(df2).fillna(0)
  12.  
     
  13.  
    print(df)

Output:

  1.  
    A B C D
  2.  
    0 10.0 20 0.0 0.0
  3.  
    1 0.0 30 40.0 50.0

41append, concat 和 combine_first 示例

  1.  
    import pandas as pd
  2.  
     
  3.  
    a = {'A'10'B'20}
  4.  
    b = {'B'30'C'40'D'50}
  5.  
     
  6.  
    df1 = pd.DataFrame(a, index=[0])
  7.  
    df2 = pd.DataFrame(b, index=[1])
  8.  
     
  9.  
    d1 = pd.DataFrame()
  10.  
    d1 = d1.append(df1)
  11.  
    d1 = d1.append(df2).fillna(0)
  12.  
    print("\n------------ append ----------------\n")
  13.  
    print(d1)
  14.  
     
  15.  
    d2 = pd.concat([df1, df2]).fillna(0)
  16.  
    print("\n------------ concat ----------------\n")
  17.  
    print(d2)
  18.  
     
  19.  
    d3 = pd.DataFrame()
  20.  
    d3 = d3.combine_first(df1).combine_first(df2).fillna(0)
  21.  
    print("\n------------ combine_first ----------------\n")
  22.  
    print(d3)

Output:

  1.  
    ------------ append ----------------
  2.  
     
  3.  
    A B C D
  4.  
    0 10.0 20 0.0 0.0
  5.  
    1 0.0 30 40.0 50.0
  6.  
     
  7.  
    ------------ concat ----------------
  8.  
     
  9.  
    A B C D
  10.  
    0 10.0 20 0.0 0.0
  11.  
    1 0.0 30 40.0 50.0
  12.  
     
  13.  
    ------------ combine_first ----------------
  14.  
     
  15.  
    A B C D
  16.  
    0 10.0 20.0 0.0 0.0
  17.  
    1 0.0 30.0 40.0 50.0

42获取行和列的平均值

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5500]],
  4.  
                      columns=['Apple''Orange''Banana''Pear'],
  5.  
                      index=['Basket1''Basket2''Basket3'])
  6.  
     
  7.  
    df['Mean Basket'] = df.mean(axis=1)
  8.  
    df.loc['Mean Fruit'] = df.mean()
  9.  
     
  10.  
    print(df)

Output:

  1.  
    Apple Orange Banana Pear Mean Basket
  2.  
    Basket1 10.000000 20.0 30.0 40.000000 25.0
  3.  
    Basket2 7.000000 14.0 21.0 28.000000 17.5
  4.  
    Basket3 5.000000 5.0 0.0 0.000000 2.5
  5.  
    Mean Fruit 7.333333 13.0 17.0 22.666667 15.0

43计算行和列的总和

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5500]],
  4.  
                      columns=['Apple''Orange''Banana''Pear'],
  5.  
                      index=['Basket1''Basket2''Basket3'])
  6.  
     
  7.  
    df['Sum Basket'] = df.sum(axis=1)
  8.  
    df.loc['Sum Fruit'] = df.sum()
  9.  
     
  10.  
    print(df)

Output:

  1.  
    Apple Orange Banana Pear Sum Basket
  2.  
    Basket1 10 20 30 40 100
  3.  
    Basket2 7 14 21 28 70
  4.  
    Basket3 5 5 0 0 10
  5.  
    Sum Fruit 22 39 51 68 180

44连接两列

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame(columns=['Name''Age'])
  4.  
     
  5.  
    df.loc[1'Name'] = 'Rocky'
  6.  
    df.loc[1'Age'] = 21
  7.  
     
  8.  
    df.loc[2'Name'] = 'Sunny'
  9.  
    df.loc[2'Age'] = 22
  10.  
     
  11.  
    df.loc[3'Name'] = 'Mark'
  12.  
    df.loc[3'Age'] = 25
  13.  
     
  14.  
    df.loc[4'Name'] = 'Taylor'
  15.  
    df.loc[4'Age'] = 28
  16.  
     
  17.  
    print('\n------------ BEFORE ----------------\n')
  18.  
    print(df)
  19.  
     
  20.  
    df['Employee'] = df['Name'].map(str) + ' - ' + df['Age'].map(str)
  21.  
    df = df.reindex(['Employee'], axis=1)
  22.  
     
  23.  
    print('\n------------ AFTER ----------------\n')
  24.  
    print(df)

Output:

  1.  
    ------------ BEFORE ----------------
  2.  
     
  3.  
    Name Age
  4.  
    1 Rocky 21
  5.  
    2 Sunny 22
  6.  
    3 Mark 25
  7.  
    4 Taylor 28
  8.  
     
  9.  
    ------------ AFTER ----------------
  10.  
     
  11.  
    Employee
  12.  
    1 Rocky - 21
  13.  
    2 Sunny - 22
  14.  
    3 Mark - 25
  15.  
    4 Taylor - 28

45过滤包含某字符串的行

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'DateOfBirth': ['1986-11-11''1999-05-12''1976-01-01',
  4.  
                                       '1986-06-01''1983-06-04''1990-03-07',
  5.  
                                       '1999-07-09'],
  6.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  7.  
                       },
  8.  
                      index=['Jane''Nick''Aaron''Penelope''Dean',
  9.  
                             'Christina''Cornelia'])
  10.  
    print(df)
  11.  
     
  12.  
    print("\n---- Filter with State contains TX ----\n")
  13.  
    df1 = df[df['State'].str.contains("TX")]
  14.  
     
  15.  
    print(df1)

Output:

  1.  
    DateOfBirth State
  2.  
    Jane 1986-11-11 NY
  3.  
    Nick 1999-05-12 TX
  4.  
    Aaron 1976-01-01 FL
  5.  
    Penelope 1986-06-01 AL
  6.  
    Dean 1983-06-04 AK
  7.  
    Christina 1990-03-07 TX
  8.  
    Cornelia 1999-07-09 TX
  9.  
     
  10.  
    ---- Filter with State contains TX ----
  11.  
     
  12.  
    DateOfBirth State
  13.  
    Nick 1999-05-12 TX
  14.  
    Christina 1990-03-07 TX
  15.  
    Cornelia 1999-07-09 TX

46过滤索引中包含某字符串的行

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'DateOfBirth': ['1986-11-11''1999-05-12''1976-01-01',
  4.  
                                       '1986-06-01''1983-06-04''1990-03-07',
  5.  
                                       '1999-07-09'],
  6.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  7.  
                       },
  8.  
                      index=['Jane''Pane''Aaron''Penelope''Frane',
  9.  
                             'Christina''Cornelia'])
  10.  
    print(df)
  11.  
    print("\n---- Filter Index contains ane ----\n")
  12.  
    df.index = df.index.astype('str')
  13.  
    df1 = df[df.index.str.contains('ane')]
  14.  
     
  15.  
    print(df1)

Output:

  1.  
    DateOfBirth State
  2.  
    Jane 1986-11-11 NY
  3.  
    Pane 1999-05-12 TX
  4.  
    Aaron 1976-01-01 FL
  5.  
    Penelope 1986-06-01 AL
  6.  
    Frane 1983-06-04 AK
  7.  
    Christina 1990-03-07 TX
  8.  
    Cornelia 1999-07-09 TX
  9.  
     
  10.  
    ---- Filter Index contains ane ----
  11.  
     
  12.  
    DateOfBirth State
  13.  
    Jane 1986-11-11 NY
  14.  
    Pane 1999-05-12 TX
  15.  
    Frane 1983-06-04 AK

47使用 AND 运算符过滤包含特定字符串值的行

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'DateOfBirth': ['1986-11-11''1999-05-12''1976-01-01',
  4.  
                                       '1986-06-01''1983-06-04''1990-03-07',
  5.  
                                       '1999-07-09'],
  6.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  7.  
                       },
  8.  
                      index=['Jane''Pane''Aaron''Penelope''Frane',
  9.  
                             'Christina''Cornelia'])
  10.  
    print(df)
  11.  
     
  12.  
    print("\n---- Filter DataFrame using & ----\n")
  13.  
     
  14.  
    df.index = df.index.astype('str')
  15.  
    df1 = df[df.index.str.contains('ane') & df['State'].str.contains("TX")]
  16.  
     
  17.  
    print(df1)

Output:

  1.  
    DateOfBirth State
  2.  
    Jane 1986-11-11 NY
  3.  
    Pane 1999-05-12 TX
  4.  
    Aaron 1976-01-01 FL
  5.  
    Penelope 1986-06-01 AL
  6.  
    Frane 1983-06-04 AK
  7.  
    Christina 1990-03-07 TX
  8.  
    Cornelia 1999-07-09 TX
  9.  
     
  10.  
    ---- Filter DataFrame using & ----
  11.  
     
  12.  
    DateOfBirth State
  13.  
    Pane 1999-05-12 TX

48查找包含某字符串的所有行

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'DateOfBirth': ['1986-11-11''1999-05-12''1976-01-01',
  4.  
                                       '1986-06-01''1983-06-04''1990-03-07',
  5.  
                                       '1999-07-09'],
  6.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  7.  
                       },
  8.  
                      index=['Jane''Pane''Aaron''Penelope''Frane',
  9.  
                             'Christina''Cornelia'])
  10.  
    print(df)
  11.  
     
  12.  
    print("\n---- Filter DataFrame using & ----\n")
  13.  
     
  14.  
    df.index = df.index.astype('str')
  15.  
    df1 = df[df.index.str.contains('ane') | df['State'].str.contains("TX")]
  16.  
     
  17.  
    print(df1)

Output:

  1.  
    DateOfBirth State
  2.  
    Jane 1986-11-11 NY
  3.  
    Pane 1999-05-12 TX
  4.  
    Aaron 1976-01-01 FL
  5.  
    Penelope 1986-06-01 AL
  6.  
    Frane 1983-06-04 AK
  7.  
    Christina 1990-03-07 TX
  8.  
    Cornelia 1999-07-09 TX
  9.  
     
  10.  
    ---- Filter DataFrame using & ----
  11.  
     
  12.  
    DateOfBirth State
  13.  
    Jane 1986-11-11 NY
  14.  
    Pane 1999-05-12 TX
  15.  
    Frane 1983-06-04 AK
  16.  
    Christina 1990-03-07 TX
  17.  
    Cornelia 1999-07-09 TX

49如果行中的值包含字符串,则创建与字符串相等的另一列

  1.  
    import pandas as pd
  2.  
    import numpy as np
  3.  
     
  4.  
    df = pd.DataFrame({
  5.  
        'EmpCode': ['Emp001''Emp002''Emp003''Emp004''Emp005'],
  6.  
        'Name': ['John''Doe''William''Spark''Mark'],
  7.  
        'Occupation': ['Chemist''Accountant''Statistician',
  8.  
                       'Statistician''Programmer'],
  9.  
        'Date Of Join': ['2018-01-25''2018-01-26''2018-01-26''2018-02-26',
  10.  
                         '2018-03-16'],
  11.  
        'Age': [2324342940]})
  12.  
     
  13.  
    df['Department'] = pd.np.where(df.Occupation.str.contains("Chemist"), "Science",
  14.  
                                   pd.np.where(df.Occupation.str.contains("Statistician"), "Economics",
  15.  
                                   pd.np.where(df.Occupation.str.contains("Programmer"), "Computer""General")))
  16.  
     
  17.  
    print(df)

Output:

  1.  
    Age Date Of Join EmpCode Name Occupation Department
  2.  
    0 23 2018-01-25 Emp001 John Chemist Science
  3.  
    1 24 2018-01-26 Emp002 Doe Accountant General
  4.  
    2 34 2018-01-26 Emp003 William Statistician Economics
  5.  
    3 29 2018-02-26 Emp004 Spark Statistician Economics
  6.  
    4 40 2018-03-16 Emp005 Mark Programmer Computer

50计算 pandas group 中每组的行数

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5500],
  4.  
                       [6666], [8888], [5500]],
  5.  
                      columns=['Apple''Orange''Rice''Oil'],
  6.  
                      index=['Basket1''Basket2''Basket3',
  7.  
                             'Basket4''Basket5''Basket6'])
  8.  
     
  9.  
    print(df)
  10.  
    print("\n ----------------------------- \n")
  11.  
    print(df[['Apple''Orange''Rice''Oil']].
  12.  
          groupby(['Apple']).agg(['mean''count']))

Output:

  1.  
    Apple Orange Rice Oil
  2.  
    Basket1 10 20 30 40
  3.  
    Basket2 7 14 21 28
  4.  
    Basket3 5 5 0 0
  5.  
    Basket4 6 6 6 6
  6.  
    Basket5 8 8 8 8
  7.  
    Basket6 5 5 0 0
  8.  
     
  9.  
    -----------------------------
  10.  
     
  11.  
    Orange Rice Oil
  12.  
    mean count mean count mean count
  13.  
    Apple
  14.  
    5 5 2 0 2 0 2
  15.  
    6 6 1 6 1 6 1
  16.  
    7 14 1 21 1 28 1
  17.  
    8 8 1 8 1 8 1
  18.  
    10 20 1 30 1 40 1

51检查字符串是否在 DataFrme 中

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'DateOfBirth': ['1986-11-11''1999-05-12''1976-01-01',
  4.  
                                       '1986-06-01''1983-06-04''1990-03-07',
  5.  
                                       '1999-07-09'],
  6.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  7.  
                       },
  8.  
                      index=['Jane''Pane''Aaron''Penelope''Frane',
  9.  
                             'Christina''Cornelia'])
  10.  
     
  11.  
    if df['State'].str.contains('TX').any():
  12.  
        print("TX is there")

Output:

TX is there
 

52从 DataFrame 列中获取唯一行值

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'State': ['NY''TX''FL''AL''AK''TX''TX']
  4.  
                       },
  5.  
                      index=['Jane''Nick''Aaron''Penelope''Dean',
  6.  
                             'Christina''Cornelia'])
  7.  
     
  8.  
    print(df)
  9.  
    print("\n----------------\n")
  10.  
     
  11.  
    print(df["State"].unique())

Output:

  1.  
    State
  2.  
    Jane NY
  3.  
    Nick TX
  4.  
    Aaron FL
  5.  
    Penelope AL
  6.  
    Dean AK
  7.  
    Christina TX
  8.  
    Cornelia TX
  9.  
     
  10.  
    ----------------
  11.  
     
  12.  
    ['NY' 'TX' 'FL' 'AL' 'AK']

53计算 DataFrame 列的不同值

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'Age': [3020224020302025],
  4.  
                        'Height': [16570120801627212481],
  5.  
                        'Score': [4.68.39.03.34893],
  6.  
                        'State': ['NY''TX''FL''AL''NY''TX''FL''AL']},
  7.  
                       index=['Jane''Nick''Aaron''Penelope''Jaane''Nicky''Armour''Ponting'])
  8.  
     
  9.  
    print(df.Age.value_counts())

Output:

  1.  
    20 3
  2.  
    30 2
  3.  
    25 1
  4.  
    22 1
  5.  
    40 1
  6.  
    Name: Age, dtype: int64

54删除具有重复索引的行

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'Age': [3030224020302025],
  4.  
                       'Height': [165165120801627212481],
  5.  
                       'Score': [4.64.69.03.34893],
  6.  
                       'State': ['NY''NY''FL''AL''NY''TX''FL''AL']},
  7.  
                      index=['Jane''Jane''Aaron''Penelope''Jaane''Nicky',
  8.  
                             'Armour''Ponting'])
  9.  
     
  10.  
    print("\n -------- Duplicate Rows ----------- \n")
  11.  
    print(df)
  12.  
     
  13.  
    df1 = df.reset_index().drop_duplicates(subset='index',
  14.  
                                           keep='first').set_index('index')
  15.  
     
  16.  
    print("\n ------- Unique Rows ------------ \n")
  17.  
    print(df1)

Output:

  1.  
    -------- Duplicate Rows -----------
  2.  
     
  3.  
    Age Height Score State
  4.  
    Jane 30 165 4.6 NY
  5.  
    Jane 30 165 4.6 NY
  6.  
    Aaron 22 120 9.0 FL
  7.  
    Penelope 40 80 3.3 AL
  8.  
    Jaane 20 162 4.0 NY
  9.  
    Nicky 30 72 8.0 TX
  10.  
    Armour 20 124 9.0 FL
  11.  
    Ponting 25 81 3.0 AL
  12.  
     
  13.  
    ------- Unique Rows ------------
  14.  
     
  15.  
    Age Height Score State
  16.  
    index
  17.  
    Jane 30 165 4.6 NY
  18.  
    Aaron 22 120 9.0 FL
  19.  
    Penelope 40 80 3.3 AL
  20.  
    Jaane 20 162 4.0 NY
  21.  
    Nicky 30 72 8.0 TX
  22.  
    Armour 20 124 9.0 FL
  23.  
    Ponting 25 81 3.0 AL

55删除某些列具有重复值的行

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'Age': [3040304030302025],
  4.  
                       'Height': [1201621201201207212081],
  5.  
                       'Score': [4.64.69.03.34893],
  6.  
                       'State': ['NY''NY''FL''AL''NY''TX''FL''AL']},
  7.  
                      index=['Jane''Jane''Aaron''Penelope''Jaane''Nicky',
  8.  
                             'Armour''Ponting'])
  9.  
     
  10.  
    print("\n -------- Duplicate Rows ----------- \n")
  11.  
    print(df)
  12.  
     
  13.  
    df1 = df.reset_index().drop_duplicates(subset=['Age','Height'],
  14.  
                                           keep='first').set_index('index')
  15.  
     
  16.  
    print("\n ------- Unique Rows ------------ \n")
  17.  
    print(df1)

Output:

  1.  
    -------- Duplicate Rows -----------
  2.  
     
  3.  
    Age Height Score State
  4.  
    Jane 30 120 4.6 NY
  5.  
    Jane 40 162 4.6 NY
  6.  
    Aaron 30 120 9.0 FL
  7.  
    Penelope 40 120 3.3 AL
  8.  
    Jaane 30 120 4.0 NY
  9.  
    Nicky 30 72 8.0 TX
  10.  
    Armour 20 120 9.0 FL
  11.  
    Ponting 25 81 3.0 AL
  12.  
     
  13.  
    ------- Unique Rows ------------
  14.  
     
  15.  
    Age Height Score State
  16.  
    index
  17.  
    Jane 30 120 4.6 NY
  18.  
    Jane 40 162 4.6 NY
  19.  
    Penelope 40 120 3.3 AL
  20.  
    Nicky 30 72 8.0 TX
  21.  
    Armour 20 120 9.0 FL
  22.  
    Ponting 25 81 3.0 AL

56从 DataFrame 单元格中获取值

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'Age': [3040304030302025],
  4.  
                       'Height': [1201621201201207212081],
  5.  
                       'Score': [4.64.69.03.34893],
  6.  
                       'State': ['NY''NY''FL''AL''NY''TX''FL''AL']},
  7.  
                      index=['Jane''Jane''Aaron''Penelope''Jaane''Nicky',
  8.  
                             'Armour''Ponting'])
  9.  
     
  10.  
    print(df.loc['Nicky''Age'])

Output:

30
 

57使用 DataFrame 中的条件索引获取单元格上的标量值

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'Age': [3040304030302025],
  4.  
                       'Height': [1201621201201207212081],
  5.  
                       'Score': [4.64.69.03.34893],
  6.  
                       'State': ['NY''NY''FL''AL''NY''TX''FL''AL']},
  7.  
                      index=['Jane''Jane''Aaron''Penelope''Jaane''Nicky',
  8.  
                             'Armour''Ponting'])
  9.  
     
  10.  
    print("\nGet Height where Age is 20")
  11.  
    print(df.loc[df['Age'] == 20'Height'].values[0])
  12.  
     
  13.  
    print("\nGet State where Age is 30")
  14.  
    print(df.loc[df['Age'] == 30'State'].values[0])

Output:

  1.  
    Get Height where Age is 20
  2.  
    120
  3.  
     
  4.  
    Get State where Age is 30
  5.  
    NY

58设置 DataFrame 的特定单元格值

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'Age': [3040304030302025],
  4.  
                       'Height': [1201621201201207212081]},
  5.  
                      index=['Jane''Jane''Aaron''Penelope''Jaane''Nicky',
  6.  
                             'Armour''Ponting'])
  7.  
    print("\n--------------Before------------\n")
  8.  
    print(df)
  9.  
     
  10.  
    df.iat[00] = 90
  11.  
    df.iat[01] = 91
  12.  
    df.iat[11] = 92
  13.  
    df.iat[21] = 93
  14.  
    df.iat[71] = 99
  15.  
     
  16.  
    print("\n--------------After------------\n")
  17.  
    print(df)

Output:

  1.  
    --------------Before------------
  2.  
     
  3.  
    Age Height
  4.  
    Jane 30 120
  5.  
    Jane 40 162
  6.  
    Aaron 30 120
  7.  
    Penelope 40 120
  8.  
    Jaane 30 120
  9.  
    Nicky 30 72
  10.  
    Armour 20 120
  11.  
    Ponting 25 81
  12.  
     
  13.  
    --------------After------------
  14.  
     
  15.  
    Age Height
  16.  
    Jane 90 91
  17.  
    Jane 40 92
  18.  
    Aaron 30 93
  19.  
    Penelope 40 120
  20.  
    Jaane 30 120
  21.  
    Nicky 30 72
  22.  
    Armour 20 120
  23.  
    Ponting 25 99

59从 DataFrame 行获取单元格值

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'Age': [3040304030302025],
  4.  
                       'Height': [1201621201201207212081]},
  5.  
                      index=['Jane''Jane''Aaron''Penelope''Jaane''Nicky',
  6.  
                             'Armour''Ponting'])
  7.  
     
  8.  
     
  9.  
    print(df.loc[df.Age == 30,'Height'].tolist())

Output:

[120, 120, 120, 72]
 

60用字典替换 DataFrame 列中的值

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'State': ['NY''TX''FL''AL''AK''TX''TX']
  4.  
                       },
  5.  
                      index=['Jane''Nick''Aaron''Penelope''Dean',
  6.  
                             'Christina''Cornelia'])
  7.  
     
  8.  
    print(df)
  9.  
     
  10.  
    dict = {"NY"1"TX"2"FL"3"AL"4"AK"5}
  11.  
    df1 = df.replace({"State": dict})
  12.  
     
  13.  
    print("\n\n")
  14.  
    print(df1)

Output:

  1.  
    State
  2.  
    Jane NY
  3.  
    Nick TX
  4.  
    Aaron FL
  5.  
    Penelope AL
  6.  
    Dean AK
  7.  
    Christina TX
  8.  
    Cornelia TX
  9.  
     
  10.  
     
  11.  
     
  12.  
    State
  13.  
    Jane 1
  14.  
    Nick 2
  15.  
    Aaron 3
  16.  
    Penelope 4
  17.  
    Dean 5
  18.  
    Christina 2
  19.  
    Cornelia 2

61统计基于某一列的一列的数值

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'DateOfBirth': ['1986-11-11''1999-05-12''1976-01-01',
  4.  
                                       '1986-06-01''1983-06-04''1990-03-07',
  5.  
                                       '1999-07-09'],                   
  6.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  7.  
                       },
  8.  
                      index=['Jane''Nick''Aaron''Penelope''Dean',
  9.  
                             'Christina''Cornelia'])
  10.  
     
  11.  
    print(df.groupby('State').DateOfBirth.nunique())

Output:

  1.  
    State
  2.  
    AK 1
  3.  
    AL 1
  4.  
    FL 1
  5.  
    NY 1
  6.  
    TX 3
  7.  
    Name: DateOfBirth, dtype: int64

62处理 DataFrame 中的缺失值

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5,]],
  4.  
                      columns=['Apple''Orange''Banana''Pear'],
  5.  
                      index=['Basket1''Basket2''Basket3'])
  6.  
     
  7.  
    print("\n--------- DataFrame ---------\n")
  8.  
    print(df)
  9.  
     
  10.  
    print("\n--------- Use of isnull() ---------\n")
  11.  
    print(df.isnull())
  12.  
     
  13.  
    print("\n--------- Use of notnull() ---------\n")
  14.  
    print(df.notnull())

Output:

  1.  
    --------- DataFrame ---------
  2.  
     
  3.  
    Apple Orange Banana Pear
  4.  
    Basket1 10 20.0 30.0 40.0
  5.  
    Basket2 7 14.0 21.0 28.0
  6.  
    Basket3 5 NaN NaN NaN
  7.  
     
  8.  
    --------- Use of isnull() ---------
  9.  
     
  10.  
    Apple Orange Banana Pear
  11.  
    Basket1 False False False False
  12.  
    Basket2 False False False False
  13.  
    Basket3 False True True True
  14.  
     
  15.  
    --------- Use of notnull() ---------
  16.  
     
  17.  
    Apple Orange Banana Pear
  18.  
    Basket1 True True True True
  19.  
    Basket2 True True True True
  20.  
    Basket3 True False False False

63删除包含任何缺失数据的行

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5,]],
  4.  
                      columns=['Apple''Orange''Banana''Pear'],
  5.  
                      index=['Basket1''Basket2''Basket3'])
  6.  
     
  7.  
    print("\n--------- DataFrame ---------\n")
  8.  
    print(df)
  9.  
     
  10.  
    print("\n--------- Use of dropna() ---------\n")
  11.  
    print(df.dropna())

Output:

  1.  
    --------- DataFrame ---------
  2.  
     
  3.  
    Apple Orange Banana Pear
  4.  
    Basket1 10 20.0 30.0 40.0
  5.  
    Basket2 7 14.0 21.0 28.0
  6.  
    Basket3 5 NaN NaN NaN
  7.  
     
  8.  
    --------- Use of dropna() ---------
  9.  
     
  10.  
    Apple Orange Banana Pear
  11.  
    Basket1 10 20.0 30.0 40.0
  12.  
    Basket2 7 14.0 21.0 28.0

64删除 DataFrame 中缺失数据的列

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5,]],
  4.  
                      columns=['Apple''Orange''Banana''Pear'],
  5.  
                      index=['Basket1''Basket2''Basket3'])
  6.  
     
  7.  
    print("\n--------- DataFrame ---------\n")
  8.  
    print(df)
  9.  
     
  10.  
    print("\n--------- Drop Columns) ---------\n")
  11.  
    print(df.dropna(1))

Output:

  1.  
    --------- DataFrame ---------
  2.  
     
  3.  
    Apple Orange Banana Pear
  4.  
    Basket1 10 20.0 30.0 40.0
  5.  
    Basket2 7 14.0 21.0 28.0
  6.  
    Basket3 5 NaN NaN NaN
  7.  
     
  8.  
    --------- Drop Columns) ---------
  9.  
     
  10.  
    Apple
  11.  
    Basket1 10
  12.  
    Basket2 7
  13.  
    Basket3 5

65按降序对索引值进行排序

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'DateOfBirth': ['1986-11-11''1999-05-12''1976-01-01',
  4.  
                                       '1986-06-01''1983-06-04''1990-03-07',
  5.  
                                       '1999-07-09'],
  6.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  7.  
                       },
  8.  
                      index=['Jane''Pane''Aaron''Penelope''Frane',
  9.  
                             'Christina''Cornelia'])
  10.  
     
  11.  
    print(df.sort_index(ascending=False))

Output:

  1.  
    DateOfBirth State
  2.  
    Penelope 1986-06-01 AL
  3.  
    Pane 1999-05-12 TX
  4.  
    Jane 1986-11-11 NY
  5.  
    Frane 1983-06-04 AK
  6.  
    Cornelia 1999-07-09 TX
  7.  
    Christina 1990-03-07 TX
  8.  
    Aaron 1976-01-01 FL

66按降序对列进行排序

  1.  
    import pandas as pd
  2.  
     
  3.  
    employees = pd.DataFrame({
  4.  
        'EmpCode': ['Emp001''Emp002''Emp003''Emp004''Emp005'],
  5.  
        'Name': ['John''Doe''William''Spark''Mark'],
  6.  
        'Occupation': ['Chemist''Statistician''Statistician',
  7.  
                       'Statistician''Programmer'],
  8.  
        'Date Of Join': ['2018-01-25''2018-01-26''2018-01-26''2018-02-26',
  9.  
                         '2018-03-16'],
  10.  
        'Age': [2324342940]})
  11.  
     
  12.  
     
  13.  
    print(employees.sort_index(axis=1, ascending=False))

Output:

  1.  
    Occupation Name EmpCode Date Of Join Age
  2.  
    0 Chemist John Emp001 2018-01-25 23
  3.  
    1 Statistician Doe Emp002 2018-01-26 24
  4.  
    2 Statistician William Emp003 2018-01-26 34
  5.  
    3 Statistician Spark Emp004 2018-02-26 29
  6.  
    4 Programmer Mark Emp005 2018-03-16 40

67使用 rank 方法查找 DataFrame 中元素的排名

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5500]],
  4.  
                      columns=['Apple''Orange''Banana''Pear'],
  5.  
                      index=['Basket1''Basket2''Basket3'])
  6.  
     
  7.  
    print("\n--------- DataFrame Values--------\n")
  8.  
    print(df)
  9.  
     
  10.  
    print("\n--------- DataFrame Values by Rank--------\n")
  11.  
    print(df.rank())

Output:

  1.  
    --------- DataFrame Values--------
  2.  
     
  3.  
    Apple Orange Banana Pear
  4.  
    Basket1 10 20 30 40
  5.  
    Basket2 7 14 21 28
  6.  
    Basket3 5 5 0 0
  7.  
     
  8.  
    --------- DataFrame Values by Rank--------
  9.  
     
  10.  
    Apple Orange Banana Pear
  11.  
    Basket1 3.0 3.0 3.0 3.0
  12.  
    Basket2 2.0 2.0 2.0 2.0
  13.  
    Basket3 1.0 1.0 1.0 1.0

68在多列上设置索引

  1.  
    import pandas as pd
  2.  
     
  3.  
    employees = pd.DataFrame({
  4.  
        'EmpCode': ['Emp001''Emp002''Emp003''Emp004''Emp005'],
  5.  
        'Name': ['John''Doe''William''Spark''Mark'],
  6.  
        'Occupation': ['Chemist''Statistician''Statistician',
  7.  
                       'Statistician''Programmer'],
  8.  
        'Date Of Join': ['2018-01-25''2018-01-26''2018-01-26''2018-02-26',
  9.  
                         '2018-03-16'],
  10.  
        'Age': [2324342940]})
  11.  
     
  12.  
    print("\n --------- Before Index ----------- \n")
  13.  
    print(employees)
  14.  
     
  15.  
    print("\n --------- Multiple Indexing ----------- \n")
  16.  
    print(employees.set_index(['Occupation''Age']))

Output:

  1.  
    Date Of Join EmpCode Name
  2.  
    Occupation Age
  3.  
    Chemist 23 2018-01-25 Emp001 John
  4.  
    Statistician 24 2018-01-26 Emp002 Doe
  5.  
    34 2018-01-26 Emp003 William
  6.  
    29 2018-02-26 Emp004 Spark
  7.  
    Programmer 40 2018-03-16 Emp005 Mark

69确定 DataFrame 的周期索引和列

  1.  
    import pandas as pd
  2.  
     
  3.  
    values = ["India""Canada""Australia",
  4.  
              "Japan""Germany""France"]
  5.  
     
  6.  
    pidx = pd.period_range('2015-01-01', periods=6)
  7.  
     
  8.  
    df = pd.DataFrame(values, index=pidx, columns=['Country'])
  9.  
     
  10.  
    print(df)

Output:

  1.  
    Country
  2.  
    2015-01-01 India
  3.  
    2015-01-02 Canada
  4.  
    2015-01-03 Australia
  5.  
    2015-01-04 Japan
  6.  
    2015-01-05 Germany
  7.  
    2015-01-06 France

70导入 CSV 指定特定索引

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.read_csv('test.csv', index_col="DateTime")
  4.  
    print(df)

Output:

  1.  
    Wheat Rice Oil
  2.  
    DateTime
  3.  
    10/10/2016 10.500 12.500 16.500
  4.  
    10/11/2016 11.250 12.750 17.150
  5.  
    10/12/2016 10.000 13.150 15.500
  6.  
    10/13/2016 12.000 14.500 16.100
  7.  
    10/14/2016 13.000 14.825 15.600
  8.  
    10/15/2016 13.075 15.465 15.315
  9.  
    10/16/2016 13.650 16.105 15.030
  10.  
    10/17/2016 14.225 16.745 14.745
  11.  
    10/18/2016 14.800 17.385 14.460
  12.  
    10/19/2016 15.375 18.025 14.175

71将 DataFrame 写入 csv

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame({'DateOfBirth': ['1986-11-11''1999-05-12''1976-01-01',
  4.  
                                       '1986-06-01''1983-06-04''1990-03-07',
  5.  
                                       '1999-07-09'],
  6.  
                       'State': ['NY''TX''FL''AL''AK''TX''TX']
  7.  
                       },
  8.  
                      index=['Jane''Pane''Aaron''Penelope''Frane',
  9.  
                             'Christina''Cornelia'])
  10.  
     
  11.  
    df.to_csv('test.csv', encoding='utf-8', index=True)

Output:

检查本地文件
 

72使用 Pandas 读取 csv 文件的特定列

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.read_csv("test.csv", usecols = ['Wheat','Oil'])
  4.  
    print(df)

73Pandas 获取 CSV 列的列表

  1.  
    import pandas as pd
  2.  
     
  3.  
    cols = list(pd.read_csv("test.csv", nrows =1))
  4.  
    print(cols)

Output:

['DateTime', 'Wheat', 'Rice', 'Oil']
 

74找到列值最大的行

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5515812]],
  4.  
                      columns=['Apple''Orange''Banana''Pear'],
  5.  
                      index=['Basket1''Basket2''Basket3'])
  6.  
     
  7.  
    print(df.ix[df['Apple'].idxmax()])

Output:

  1.  
    Apple 55
  2.  
    Orange 15
  3.  
    Banana 8
  4.  
    Pear 12
  5.  
    Name: Basket3, dtype: int64

75使用查询方法进行复杂条件选择

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5515812]],
  4.  
                      columns=['Apple''Orange''Banana''Pear'],
  5.  
                      index=['Basket1''Basket2''Basket3'])
  6.  
     
  7.  
    print(df)
  8.  
     
  9.  
    print("\n ----------- Filter data using query method ------------- \n")
  10.  
    df1 = df.ix[df.query('Apple > 50 & Orange <= 15 & Banana < 15 & Pear == 12').index]
  11.  
    print(df1)

Output:

  1.  
    Apple Orange Banana Pear
  2.  
    Basket1 10 20 30 40
  3.  
    Basket2 7 14 21 28
  4.  
    Basket3 55 15 8 12
  5.  
     
  6.  
    ----------- Filter data using query method -------------
  7.  
     
  8.  
    Apple Orange Banana Pear
  9.  
    Basket3 55 15 8 12

76检查 Pandas 中是否存在列

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5515812]],
  4.  
                      columns=['Apple''Orange''Banana''Pear'],
  5.  
                      index=['Basket1''Basket2''Basket3'])
  6.  
     
  7.  
    if 'Apple' in df.columns:
  8.  
        print("Yes")
  9.  
    else:
  10.  
        print("No")
  11.  
     
  12.  
     
  13.  
    if set(['Apple','Orange']).issubset(df.columns):
  14.  
        print("Yes")
  15.  
    else:
  16.  
        print("No")

77为特定列从 DataFrame 中查找 n-smallest 和 n-largest 值

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5515812],
  4.  
                       [151418], [7118], [5492]],
  5.  
                      columns=['Apple''Orange''Banana''Pear'],
  6.  
                      index=['Basket1''Basket2''Basket3''Basket4',
  7.  
                             'Basket5''Basket6'])
  8.  
     
  9.  
    print("\n----------- nsmallest -----------\n")
  10.  
    print(df.nsmallest(2, ['Apple']))
  11.  
     
  12.  
    print("\n----------- nlargest -----------\n")
  13.  
    print(df.nlargest(2, ['Apple']))

Output:

  1.  
    ----------- nsmallest -----------
  2.  
     
  3.  
    Apple Orange Banana Pear
  4.  
    Basket6 5 4 9 2
  5.  
    Basket2 7 14 21 28
  6.  
     
  7.  
    ----------- nlargest -----------
  8.  
     
  9.  
    Apple Orange Banana Pear
  10.  
    Basket3 55 15 8 12
  11.  
    Basket4 15 14 1 8

78从 DataFrame 中查找所有列的最小值和最大值

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5515812],
  4.  
                       [151418], [7118], [5492]],
  5.  
                      columns=['Apple''Orange''Banana''Pear'],
  6.  
                      index=['Basket1''Basket2''Basket3''Basket4',
  7.  
                             'Basket5''Basket6'])
  8.  
     
  9.  
    print("\n----------- Minimum -----------\n")
  10.  
    print(df[['Apple''Orange''Banana''Pear']].min())
  11.  
     
  12.  
    print("\n----------- Maximum -----------\n")
  13.  
    print(df[['Apple''Orange''Banana''Pear']].max())

Output:

  1.  
    ----------- Minimum -----------
  2.  
     
  3.  
    Apple 5
  4.  
    Orange 1
  5.  
    Banana 1
  6.  
    Pear 2
  7.  
    dtype: int64
  8.  
     
  9.  
    ----------- Maximum -----------
  10.  
     
  11.  
    Apple 55
  12.  
    Orange 20
  13.  
    Banana 30
  14.  
    Pear 40
  15.  
    dtype: int64

79在 DataFrame 中找到最小值和最大值所在的索引位置

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5515812],
  4.  
                       [151418], [7118], [5492]],
  5.  
                      columns=['Apple''Orange''Banana''Pear'],
  6.  
                      index=['Basket1''Basket2''Basket3''Basket4',
  7.  
                             'Basket5''Basket6'])
  8.  
     
  9.  
    print("\n----------- Minimum -----------\n")
  10.  
    print(df[['Apple''Orange''Banana''Pear']].idxmin())
  11.  
     
  12.  
    print("\n----------- Maximum -----------\n")
  13.  
    print(df[['Apple''Orange''Banana''Pear']].idxmax())

Output:

  1.  
    ----------- Minimum -----------
  2.  
     
  3.  
    Apple Basket6
  4.  
    Orange Basket5
  5.  
    Banana Basket4
  6.  
    Pear Basket6
  7.  
    dtype: object
  8.  
     
  9.  
    ----------- Maximum -----------
  10.  
     
  11.  
    Apple Basket3
  12.  
    Orange Basket1
  13.  
    Banana Basket1
  14.  
    Pear Basket1
  15.  
    dtype: object

80计算 DataFrame Columns 的累积乘积和累积总和

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5515812],
  4.  
                       [151418], [7118], [5492]],
  5.  
                      columns=['Apple''Orange''Banana''Pear'],
  6.  
                      index=['Basket1''Basket2''Basket3''Basket4',
  7.  
                             'Basket5''Basket6'])
  8.  
     
  9.  
    print("\n----------- Cumulative Product -----------\n")
  10.  
    print(df[['Apple''Orange''Banana''Pear']].cumprod())
  11.  
     
  12.  
    print("\n----------- Cumulative Sum -----------\n")
  13.  
    print(df[['Apple''Orange''Banana''Pear']].cumsum())

Output:

  1.  
    ----------- Cumulative Product -----------
  2.  
     
  3.  
    Apple Orange Banana Pear
  4.  
    Basket1 10 20 30 40
  5.  
    Basket2 70 280 630 1120
  6.  
    Basket3 3850 4200 5040 13440
  7.  
    Basket4 57750 58800 5040 107520
  8.  
    Basket5 404250 58800 5040 860160
  9.  
    Basket6 2021250 235200 45360 1720320
  10.  
     
  11.  
    ----------- Cumulative Sum -----------
  12.  
     
  13.  
    Apple Orange Banana Pear
  14.  
    Basket1 10 20 30 40
  15.  
    Basket2 17 34 51 68
  16.  
    Basket3 72 49 59 80
  17.  
    Basket4 87 63 60 88
  18.  
    Basket5 94 64 61 96
  19.  
    Basket6 99 68 70 98

81汇总统计

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5515812],
  4.  
                       [151418], [7118], [5492]],
  5.  
                      columns=['Apple''Orange''Banana''Pear'],
  6.  
                      index=['Basket1''Basket2''Basket3''Basket4',
  7.  
                             'Basket5''Basket6'])
  8.  
     
  9.  
    print("\n----------- Describe DataFrame -----------\n")
  10.  
    print(df.describe())
  11.  
     
  12.  
    print("\n----------- Describe Column -----------\n")
  13.  
    print(df[['Apple']].describe())

Output:

  1.  
    ----------- Describe DataFrame -----------
  2.  
     
  3.  
    Apple Orange Banana Pear
  4.  
    count 6.000000 6.000000 6.000000 6.000000
  5.  
    mean 16.500000 11.333333 11.666667 16.333333
  6.  
    std 19.180719 7.257180 11.587349 14.555640
  7.  
    min 5.000000 1.000000 1.000000 2.000000
  8.  
    25% 7.000000 6.500000 2.750000 8.000000
  9.  
    50% 8.500000 14.000000 8.500000 10.000000
  10.  
    75% 13.750000 14.750000 18.000000 24.000000
  11.  
    max 55.000000 20.000000 30.000000 40.000000
  12.  
     
  13.  
    ----------- Describe Column -----------
  14.  
     
  15.  
    Apple
  16.  
    count 6.000000
  17.  
    mean 16.500000
  18.  
    std 19.180719
  19.  
    min 5.000000
  20.  
    25% 7.000000
  21.  
    50% 8.500000
  22.  
    75% 13.750000
  23.  
    max 55.000000

82查找 DataFrame 的均值、中值和众数

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5515812],
  4.  
                       [151418], [7118], [5492]],
  5.  
                      columns=['Apple''Orange''Banana''Pear'],
  6.  
                      index=['Basket1''Basket2''Basket3''Basket4',
  7.  
                             'Basket5''Basket6'])
  8.  
     
  9.  
    print("\n----------- Calculate Mean -----------\n")
  10.  
    print(df.mean())
  11.  
     
  12.  
    print("\n----------- Calculate Median -----------\n")
  13.  
    print(df.median())
  14.  
     
  15.  
    print("\n----------- Calculate Mode -----------\n")
  16.  
    print(df.mode())

Output:

  1.  
    ----------- Calculate Mean -----------
  2.  
     
  3.  
    Apple 16.500000
  4.  
    Orange 11.333333
  5.  
    Banana 11.666667
  6.  
    Pear 16.333333
  7.  
    dtype: float64
  8.  
     
  9.  
    ----------- Calculate Median -----------
  10.  
     
  11.  
    Apple 8.5
  12.  
    Orange 14.0
  13.  
    Banana 8.5
  14.  
    Pear 10.0
  15.  
    dtype: float64
  16.  
     
  17.  
    ----------- Calculate Mode -----------
  18.  
     
  19.  
    Apple Orange Banana Pear
  20.  
    0 7 14 1 8

83测量 DataFrame 列的方差和标准偏差

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5515812],
  4.  
                       [151418], [7118], [5492]],
  5.  
                      columns=['Apple''Orange''Banana''Pear'],
  6.  
                      index=['Basket1''Basket2''Basket3''Basket4',
  7.  
                             'Basket5''Basket6'])
  8.  
     
  9.  
    print("\n----------- Calculate Mean -----------\n")
  10.  
    print(df.mean())
  11.  
     
  12.  
    print("\n----------- Calculate Median -----------\n")
  13.  
    print(df.median())
  14.  
     
  15.  
    print("\n----------- Calculate Mode -----------\n")
  16.  
    print(df.mode())

Output:

  1.  
    ----------- Measure Variance -----------
  2.  
     
  3.  
    Apple 367.900000
  4.  
    Orange 52.666667
  5.  
    Banana 134.266667
  6.  
    Pear 211.866667
  7.  
    dtype: float64
  8.  
     
  9.  
    ----------- Standard Deviation -----------
  10.  
     
  11.  
    Apple 19.180719
  12.  
    Orange 7.257180
  13.  
    Banana 11.587349
  14.  
    Pear 14.555640
  15.  
    dtype: float64

84计算 DataFrame 列之间的协方差

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5515812],
  4.  
                       [151418], [7118], [5492]],
  5.  
                      columns=['Apple''Orange''Banana''Pear'],
  6.  
                      index=['Basket1''Basket2''Basket3''Basket4',
  7.  
                             'Basket5''Basket6'])
  8.  
     
  9.  
    print("\n----------- Calculating Covariance -----------\n")
  10.  
    print(df.cov())
  11.  
     
  12.  
    print("\n----------- Between 2 columns -----------\n")
  13.  
    # Covariance of Apple vs Orange
  14.  
    print(df.Apple.cov(df.Orange))

Output:

  1.  
    ----------- Calculating Covariance -----------
  2.  
     
  3.  
    Apple Orange Banana Pear
  4.  
    Apple 367.9 47.600000 -40.200000 -35.000000
  5.  
    Orange 47.6 52.666667 54.333333 77.866667
  6.  
    Banana -40.2 54.333333 134.266667 154.933333
  7.  
    Pear -35.0 77.866667 154.933333 211.866667
  8.  
     
  9.  
    ----------- Between 2 columns -----------
  10.  
     
  11.  
    47.60000000000001

85计算 Pandas 中两个 DataFrame 对象之间的相关性

  1.  
    import pandas as pd
  2.  
     
  3.  
    df1 = pd.DataFrame([[10203040], [7142128], [5515812],
  4.  
                       [151418], [7118], [5492]],
  5.  
                      columns=['Apple''Orange''Banana''Pear'],
  6.  
                      index=['Basket1''Basket2''Basket3''Basket4',
  7.  
                             'Basket5''Basket6'])
  8.  
     
  9.  
    print("\n------ Calculating Correlation of one DataFrame Columns -----\n")
  10.  
    print(df1.corr())
  11.  
     
  12.  
    df2 = pd.DataFrame([[52545841], [14245178], [5515812],
  13.  
                       [151418], [7171898], [15342952]],
  14.  
                      columns=['Apple''Orange''Banana''Pear'],
  15.  
                      index=['Basket1''Basket2''Basket3''Basket4',
  16.  
                             'Basket5''Basket6'])
  17.  
     
  18.  
    print("\n----- Calculating correlation between two DataFrame -------\n")
  19.  
    print(df2.corrwith(other=df1))

Output:

  1.  
    ------ Calculating Correlation of one DataFrame Columns -----
  2.  
     
  3.  
    Apple Orange Banana Pear
  4.  
    Apple 1.000000 0.341959 -0.180874 -0.125364
  5.  
    Orange 0.341959 1.000000 0.646122 0.737144
  6.  
    Banana -0.180874 0.646122 1.000000 0.918606
  7.  
    Pear -0.125364 0.737144 0.918606 1.000000
  8.  
     
  9.  
    ----- Calculating correlation between two DataFrame -------
  10.  
     
  11.  
    Apple 0.678775
  12.  
    Orange 0.354993
  13.  
    Banana 0.920872
  14.  
    Pear 0.076919
  15.  
    dtype: float64

86计算 DataFrame 列的每个单元格的百分比变化

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[10203040], [7142128], [5515812],
  4.  
                       [151418], [7118], [5492]],
  5.  
                      columns=['Apple''Orange''Banana''Pear'],
  6.  
                      index=['Basket1''Basket2''Basket3''Basket4',
  7.  
                             'Basket5''Basket6'])
  8.  
     
  9.  
    print("\n------ Percent change at each cell of a Column -----\n")
  10.  
    print(df[['Apple']].pct_change()[:3])
  11.  
     
  12.  
    print("\n------ Percent change at each cell of a DataFrame -----\n")
  13.  
    print(df.pct_change()[:5])

Output:

  1.  
    ------ Percent change at each cell of a Column -----
  2.  
     
  3.  
    Apple
  4.  
    Basket1 NaN
  5.  
    Basket2 -0.300000
  6.  
    Basket3 6.857143
  7.  
     
  8.  
    ------ Percent change at each cell of a DataFrame -----
  9.  
     
  10.  
    Apple Orange Banana Pear
  11.  
    Basket1 NaN NaN NaN NaN
  12.  
    Basket2 -0.300000 -0.300000 -0.300000 -0.300000
  13.  
    Basket3 6.857143 0.071429 -0.619048 -0.571429
  14.  
    Basket4 -0.727273 -0.066667 -0.875000 -0.333333
  15.  
    Basket5 -0.533333 -0.928571 0.000000 0.000000

87在 Pandas 中向前和向后填充 DataFrame 列的缺失值

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[103040], [], [15812],
  4.  
                       [151418], [78], [541]],
  5.  
                      columns=['Apple''Orange''Banana''Pear'],
  6.  
                      index=['Basket1''Basket2''Basket3''Basket4',
  7.  
                             'Basket5''Basket6'])
  8.  
     
  9.  
    print("\n------ DataFrame with NaN -----\n")
  10.  
    print(df)
  11.  
     
  12.  
    print("\n------ DataFrame with Forward Filling -----\n")
  13.  
    print(df.ffill())
  14.  
     
  15.  
    print("\n------ DataFrame with Forward Filling -----\n")
  16.  
    print(df.bfill())

Output:

  1.  
    ------ DataFrame with NaN -----
  2.  
     
  3.  
    Apple Orange Banana Pear
  4.  
    Basket1 10.0 30.0 40.0 NaN
  5.  
    Basket2 NaN NaN NaN NaN
  6.  
    Basket3 15.0 8.0 12.0 NaN
  7.  
    Basket4 15.0 14.0 1.0 8.0
  8.  
    Basket5 7.0 8.0 NaN NaN
  9.  
    Basket6 5.0 4.0 1.0 NaN
  10.  
     
  11.  
    ------ DataFrame with Forward Filling -----
  12.  
     
  13.  
    Apple Orange Banana Pear
  14.  
    Basket1 10.0 30.0 40.0 NaN
  15.  
    Basket2 10.0 30.0 40.0 NaN
  16.  
    Basket3 15.0 8.0 12.0 NaN
  17.  
    Basket4 15.0 14.0 1.0 8.0
  18.  
    Basket5 7.0 8.0 1.0 8.0
  19.  
    Basket6 5.0 4.0 1.0 8.0
  20.  
     
  21.  
    ------ DataFrame with Forward Filling -----
  22.  
     
  23.  
    Apple Orange Banana Pear
  24.  
    Basket1 10.0 30.0 40.0 8.0
  25.  
    Basket2 15.0 8.0 12.0 8.0
  26.  
    Basket3 15.0 8.0 12.0 8.0
  27.  
    Basket4 15.0 14.0 1.0 8.0
  28.  
    Basket5 7.0 8.0 1.0 NaN
  29.  
    Basket6 5.0 4.0 1.0 NaN

88在 Pandas 中使用非分层索引使用 Stacking

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[103040], [], [15812],
  4.  
                       [151418], [78], [541]],
  5.  
                      columns=['Apple''Orange''Banana''Pear'],
  6.  
                      index=['Basket1''Basket2''Basket3''Basket4',
  7.  
                             'Basket5''Basket6'])
  8.  
     
  9.  
    print("\n------ DataFrame-----\n")
  10.  
    print(df)
  11.  
     
  12.  
    print("\n------ Stacking DataFrame -----\n")
  13.  
    print(df.stack(level=-1))

Output:

  1.  
    ------ DataFrame-----
  2.  
     
  3.  
    Apple Orange Banana Pear
  4.  
    Basket1 10.0 30.0 40.0 NaN
  5.  
    Basket2 NaN NaN NaN NaN
  6.  
    Basket3 15.0 8.0 12.0 NaN
  7.  
    Basket4 15.0 14.0 1.0 8.0
  8.  
    Basket5 7.0 8.0 NaN NaN
  9.  
    Basket6 5.0 4.0 1.0 NaN
  10.  
     
  11.  
    ------ Stacking DataFrame -----
  12.  
     
  13.  
    Basket1 Apple 10.0
  14.  
    Orange 30.0
  15.  
    Banana 40.0
  16.  
    Basket3 Apple 15.0
  17.  
    Orange 8.0
  18.  
    Banana 12.0
  19.  
    Basket4 Apple 15.0
  20.  
    Orange 14.0
  21.  
    Banana 1.0
  22.  
    Pear 8.0
  23.  
    Basket5 Apple 7.0
  24.  
    Orange 8.0
  25.  
    Basket6 Apple 5.0
  26.  
    Orange 4.0
  27.  
    Banana 1.0
  28.  
    dtype: float64

89使用分层索引对 Pandas 进行拆分

  1.  
    import pandas as pd
  2.  
     
  3.  
    df = pd.DataFrame([[103040], [], [15812],
  4.  
                       [151418], [78], [541]],
  5.  
                      columns=['Apple''Orange''Banana''Pear'],
  6.  
                      index=['Basket1''Basket2''Basket3''Basket4',
  7.  
                             'Basket5''Basket6'])
  8.  
     
  9.  
    print("\n------ DataFrame-----\n")
  10.  
    print(df)
  11.  
     
  12.  
    print("\n------ Unstacking DataFrame -----\n")
  13.  
    print(df.unstack(level=-1))

Output:

  1.  
    ------ DataFrame-----
  2.  
     
  3.  
    Apple Orange Banana Pear
  4.  
    Basket1 10.0 30.0 40.0 NaN
  5.  
    Basket2 NaN NaN NaN NaN
  6.  
    Basket3 15.0 8.0 12.0 NaN
  7.  
    Basket4 15.0 14.0 1.0 8.0
  8.  
    Basket5 7.0 8.0 NaN NaN
  9.  
    Basket6 5.0 4.0 1.0 NaN
  10.  
     
  11.  
    ------ Unstacking DataFrame -----
  12.  
     
  13.  
    Apple Basket1 10.0
  14.  
    Basket2 NaN
  15.  
    Basket3 15.0
  16.  
    Basket4 15.0
  17.  
    Basket5 7.0
  18.  
    Basket6 5.0
  19.  
    Orange Basket1 30.0
  20.  
    Basket2 NaN
  21.  
    Basket3 8.0
  22.  
    Basket4 14.0
  23.  
    Basket5 8.0
  24.  
    Basket6 4.0
  25.  
    Banana Basket1 40.0
  26.  
    Basket2 NaN
  27.  
    Basket3 12.0
  28.  
    Basket4 1.0
  29.  
    Basket5 NaN
  30.  
    Basket6 1.0
  31.  
    Pear Basket1 NaN
  32.  
    Basket2 NaN
  33.  
    Basket3 NaN
  34.  
    Basket4 8.0
  35.  
    Basket5 NaN
  36.  
    Basket6 NaN
  37.  
    dtype: float64

90Pandas 获取 HTML 页面上 table 数据

  1.  
    import pandas as pd
  2.  
    df pd.read_html("url")

END -


对比Excel系列图书累积销量达15w册,让你轻松掌握数据分析技能,可以在全网搜索书名进行了解:
 
posted @ 2022-10-18 20:20  Livingdying  阅读(57)  评论(0)    收藏  举报