Pandas DataFrame DataFrame.sample() 函数
    
    
            Minahil Noor
    2023年1月30日
    
    Pandas
    Pandas DataFrame
    
- 
          
            pandas.DataFrame.sample()语法
- 
          
            示例代码:DataFrame.sample()
- 
          
            示例代码:DataFrame.sample()提取列
- 
          
            示例代码:DataFrame.sample()生成数据的一部分
- 
          
            示例代码:DataFrame.sample()对 DataFrame 进行过采样
- 
          
            示例代码:DataFrame.sample()和weights
 
Python Pandas DataFrame.sample() 函数从一个 DataFrame 中随机生成一行或一列的样本。样本可以包含多行或多列。
pandas.DataFrame.sample() 语法
DataFrame.sample(
    n=None, frac=None, replace=False, weights=None, random_state=None, axis=None
)
参数
| n | 它是一个整数值。它代表要从 DataFrame中选择的行或列的随机数 | 
| frac | 它是一个浮点数值。它指定了要从 DataFrame中提取的随机行或列的百分比。例如,frac=0.45意味着选择的随机行或列将是原始数据的 45% | 
| replace | 它是一个布尔值。如果它被设置为 True,那么它将返回替换数据的样本 | 
| weights | 它是一个字符串或一个 N 维的数组结构。如果在 DataFrame上调用它,那么当轴为 0 时,它接受一列的名称,权重列中数值较大的行更有可能作为样本数据返回 | 
| random_state | 它是一个整数或 numpy.random.RandomState函数。如果它是一个整数,那么它在每次迭代中返回相同数量的行或列。否则,它返回一个numpy RandomState对象 | 
| axis | 它是一个整数或字符串。它告诉目标轴的行或列。它可以是 0 或 index,1 或columns | 
返回值
它返回一个 Series 或 DataFrame。返回的 Series 或 DataFrame 是一个调用器,包含从原始 DataFrame 中随机选择的 n 个元素。
示例代码:DataFrame.sample()
默认情况下,函数返回一个包含行的样本,即 axis=1。
import pandas as pd
dataframe=pd.DataFrame({'Attendance': {0: 60, 1: 100, 2: 80,3: 75, 4: 95},
                    'Name': {0: 'Olivia', 1: 'John', 2: 'Laura',3: 'Ben',4: 'Kevin'},
                    'Obtained Marks': {0: 56, 1: 75, 2: 82, 3: 64, 4: 67}})
print(dataframe)
我们的 DataFrame 为,
   Attendance    Name  Obtained Marks
0          60  Olivia              56
1         100    John              75
2          80   Laura              82
3          75     Ben              64
4          95   Kevin              67
这个函数的所有参数都是可选的。如果我们在执行这个函数时没有传递任何参数,它将返回一个随机的行作为输出。
import pandas as pd
dataframe = pd.DataFrame(
    {
        "Attendance": {0: 60, 1: 100, 2: 80, 3: 75, 4: 95},
        "Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
        "Obtained Marks": {0: 56, 1: 75, 2: 82, 3: 64, 4: 67},
    }
)
dataframe1 = dataframe.sample()
print(dataframe1)
输出 1:
   Attendance Name  Obtained Marks
3          75  Ben              64
输出 2:
   Attendance   Name  Obtained Marks
4          95  Kevin              67
输出 1 和输出 2 显示了同一个程序的两次执行情况。每次这个函数都会从给定的 DataFrame 中产生一个随机的行样本。
示例代码:DataFrame.sample() 提取列
要在样本中生成列,我们将简单地把我们的轴改为 1。
import pandas as pd
dataframe = pd.DataFrame(
    {
        "Attendance": {0: 60, 1: 100, 2: 80, 3: 75, 4: 95},
        "Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
        "Obtained Marks": {0: 56, 1: 75, 2: 82, 3: 64, 4: 67},
    }
)
dataframe1 = dataframe.sample(n=1, axis=1)
print(dataframe1)
输出:
     Name
0  Olivia
1    John
2   Laura
3     Ben
4   Kevin
该函数已经生成了一个单一列的样本作为输出。列的数量由参数 n=1 设置。
示例代码:DataFrame.sample() 生成数据的一部分
import pandas as pd
dataframe = pd.DataFrame(
    {
        "Attendance": {0: 60, 1: 100, 2: 80, 3: 75, 4: 95},
        "Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
        "Obtained Marks": {0: 56, 1: 75, 2: 82, 3: 64, 4: 67},
    }
)
dataframe1 = dataframe.sample(frac=0.5)
print(dataframe1)
输出:
   Attendance   Name  Obtained Marks
3          75    Ben              64
4          95  Kevin              67
1         100   John              75
返回的样本是原始数据的 50%。
示例代码:DataFrame.sample() 对 DataFrame 进行过采样
如果 frac>1,那么参数 replace 应该是 True,以允许同一行可以被多次取样,否则,它将引发 ValueError。
import pandas as pd
dataframe = pd.DataFrame(
    {
        "Attendance": {0: 60, 1: 100, 2: 80, 3: 75, 4: 95},
        "Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
        "Obtained Marks": {0: 56, 1: 75, 2: 82, 3: 64, 4: 67},
    }
)
dataframe1 = dataframe.sample(frac=1.5, replace=True)
print(dataframe1)
输出:
   Attendance   Name  Obtained Marks
3          75     Ben              64
0          60  Olivia              56
1         100    John              75
2          80   Laura              82
1         100    John              75
2          80   Laura              82
0          60  Olivia              56
4          95   Kevin              67
如果 replace 被设置为 False,同时 frac 大于 1,则会产生 ValueError。
import pandas as pd
dataframe = pd.DataFrame(
    {
        "Attendance": {0: 60, 1: 100, 2: 80, 3: 75, 4: 95},
        "Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
        "Obtained Marks": {0: 56, 1: 75, 2: 82, 3: 64, 4: 67},
    }
)
dataframe1 = dataframe.sample(frac=1.5, replace=False)
print(dataframe1)
输出:
Traceback (most recent call last):
  File "..\test.py", line 6, in <module>
    dataframe1 = dataframe.sample(frac=1.5, replace=False)
  File "..\lib\site-packages\pandas\core\generic.py", line 5044, in sample
    raise ValueError(
ValueError: Replace has to be set to `True` when upsampling the population `frac` > 1.
示例代码:DataFrame.sample() 和 weights
import pandas as pd
dataframe = pd.DataFrame(
    {
        "Attendance": {0: 60, 1: 100, 2: 80, 3: 75, 4: 95},
        "Name": {0: "Olivia", 1: "John", 2: "Laura", 3: "Ben", 4: "Kevin"},
        "Obtained Marks": {0: 56, 1: 75, 2: 82, 3: 64, 4: 67},
    }
)
dataframe1 = dataframe.sample(n=2, weights="Attendance")
print(dataframe1)
输出:
   Attendance   Name  Obtained Marks
1         100   John              75
4          95  Kevin              67
这里,在返回的样本中选择 Attendance 列中数值较大的行。
        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe